2

これは私の最初の投稿なので、自分自身を明確にしていない場合は、喜んで詳細を提供します。Javaを使用してルーティングAPIを作成しています。ファイルの大部分は、一部を正しく解析しています。私が興味を持っているXMLの部分は次のようになります。

<way id="30184957" user="Central America" uid="69853" visible="true" version="2" changeset="4961491" timestamp="2010-06-11T10:52:19Z">
  <nd ref="332597303"/>
  <nd ref="332597551"/>
  <nd ref="332597552"/>
  <tag k="highway" v="residential"/>
  <tag k="name" v="Rae's Court"/>
</way>
</b>

私のコードの関連部分は次のようになります。

public void startElement(String uri, String localName, String qName, Attributes attributes) 
{
    if (qName == "node") //if the tag value is node
    {   
        Node currentNode = new Node(0, 0, 0); //new node with all 0 values
        currentNode.nodeID = Integer.parseInt(attributes.getValue(0)); //set the ID to the id of the node
        currentNode.nodeLat = Double.parseDouble(attributes.getValue(1)); //set the Lat to the Lat of the node
        currentNode.nodeLong = Double.parseDouble(attributes.getValue(2)); //set the Long to the Long of the node
        allNodes.add(currentNode);
    }       

    if (qName == "way") //if tag value is way
    {
        currentWay = new Way(0, null); //create a new way with 0 values
        currentWay.wayID = Integer.parseInt(attributes.getValue(0)); //set the way id to the id of the way
    //  
    }

    if (qName == "nd") //if tag value is nd
    {
        Node searchNode = getNodeByID(Integer.parseInt(attributes.getValue(0))); //use getNodeByID method to check if
        currentWay.containedNodes.add(searchNode);
    }
}

私の問題:

IDとそれに含まれるノードのリスト(ndタグ)を含むwayオブジェクトを作成しようとしています。ndタグは、以前に正常に作成されたノードオブジェクトへの単なる参照です。現在、2つのArrayListを使用しています。1つはノード用で、もう1つはウェイ用です。ただし、このgetNodeByID()メソッドは、ndに到達するたびにリストを検索する必要があり、より大きなXMLファイルを探すのに大幅な速度低下を引き起こします。

ndをその方法で読み取る方法を見つけることができないようで、代わりに別のifステートメントでそれらを検索する必要があります。

方法を見つけて、同じステートメントですべてのndがそれに関連する方法はありますか?もしそうなら、それらの配列リストをハッシュマップに変更することを計画しているので、オブジェクトの作成がはるかに簡単になります。

はっきりしていなくてすみません...これらの問題をテキストで説明するのはあまり得意ではありません。

4

2 に答える 2

0

検索するノードを に格納する代わりにList、 を使用しMap<Integer,Node>ます。そうすれば、検索は O(n) ではなく O(1) になります。それらを入力順に並べる必要がある場合はList、後で使用するためにそれらを a にも追加しますがMap、検索には を使用してください。

それ以外の

allNodes.add(currentNode);

あなたが持っているだろう

allNodes.put(currentNode.nodeId, currentNode);
于 2012-11-03T00:44:16.810 に答える
0

(qname=="nd")のコードをstartElement メソッドではなく endElement メソッドに挿入します。

于 2012-11-03T01:21:43.257 に答える