2009-06-23 9 views
7

XML文書に「person」要素が含まれているかどうかを確認したいのですが、すべてのXMLノード世代をJava DOMで反復する

NodeList nodeList = root.getChildNodes(); 
for(int i=0; i<nodeList.getLength(); i++){ 
    Node childNode = nodeList.item(i); 
    if (childNode.getNodeName() == "person") { 
    //do something with it 
    } 
} 

をそして、私はサブ要素に行くために複数のループを追加することができますが、私はどのように遠くに決定することで入れてどのように多くのネストされたループを知っている必要があります:私は非常に単純にすべての第一世代の要素を確認することができますドリルする文書。私は10個のループを入れ子にして、与えられた文書の12個の要素を入れ子にしたperson要素で終わることができます。私はそれがどれほど深く入れ子になっていても要素を引き出すことができる必要があります。

文書全体から要素を収穫する方法はありますか?同様に、すべてのタグのテキスト値を配列として返すか、それを繰り返しますか?

おそらく、PythonのElementTreeの 'のfindAll' メソッドに似て何か:mmyers状態として

for person in tree.findall('//person'): 
    personlist.append(person) 
+1

私はあなたが必要だと思うhttp://en.wikipedia.org/wiki/Recursion_%28computer_science %29。 –

答えて

10

、あなたがこの問題のために再帰を使用することができます。

doSomethingWithAll(root.getChildNodes()); 

void doSomethingWithAll(NodeList nodeList) 
{ 
    for (int i = 0; i < nodeList.getLength(); i++) { 
     Node childNode = nodeList.item(i); 
     if (childNode.getNodeName().equals("person")) { 
      //do something with it 
     } 

     NodeList children = childNode.getChildNodes(); 
     if (children != null) 
     { 
      doSomethingWithAll(children); 
     } 
    } 
} 
4

これがXPathの目的です。

//person 

は、直接JDKのXPath APIを使用して痛みを伴うことができます:「人」という名前のすべての要素を取得するには、ここに表現です。私は実用的なXMLライブラリに書いたラッパーを好む:http://practicalxml.sourceforge.net/

そして、ここでは、私が書いたチュートリアルです(一般的にはJDKのXPathには、しかしXPathWrapperに言及):http://www.kdgregory.com/index.php?page=xml.xpath

10

私は3つのpossiblities(の2を参照してください他の人は答えました):

  1. 再帰を使用してください。
  2. XPathを使用してください(はこの問題ではありますが、 は間違いなく探索するものです)。 というようなクエリがたくさんある場合は、 これについてkdgregoryのヘルプを使用してください。 a apiで簡単に見て を使用するのはちょっと痛いです。
  3. あなたが持っているものは(rootDocumentであればそれはある)、実際にDocumentある場合は、Document.getElementsByTagName()またはXPath別に Document.getElementsByTagName
+1

+1 - #3は間違いなく最も簡単なアプローチです。 – kdgregory

+0

+1 – NemoStein

0

を使用することができます、また、ライブラリをjOOXを使用することができますより簡単なXMLアクセスと操作のために作成したものです。 jOOXは標準のJava APIをラップし、jqueryのようなユーティリティメソッドを追加します。あなたのPythonのコードスニペットは、このJavaコードに変換します:

ここ
// Just looking for tag names 
for (Element person : $(tree).find("person")) { 
    personlist.append(person); 
} 

// Use XPath for more elaborate queries 
for (Element person : $(tree).xpath("//person")) { 
    personlist.append(person); 
} 
2

がフォーマットされたバージョンです:

Element root = xmlData.getDocumentElement(); 
NodeList children = root.getChildNodes(); 

public void doSomethingWithAllToConsole(NodeList nodeList, String tabs) 
{ 
    for(int i=0; i<nodeList.getLength(); i++){ 

     //print current node & values 
     Node childNode = nodeList.item(i); 
     if(childNode.getNodeType()==Node.ELEMENT_NODE){ 
      System.out.print(tabs + childNode.getNodeName()); 
      if(childNode.getFirstChild()!=null 
        && childNode.getFirstChild().getNodeType()==Node.TEXT_NODE 
        && !StringUtil.isNullOrEmpty(childNode.getFirstChild().getNodeValue())){ 
       System.out.print(" = " + childNode.getFirstChild().getNodeValue()); 
      } 
      System.out.println(); 
     } 

     //recursively iterate through child nodes 
     NodeList children = childNode.getChildNodes(); 
     if (children != null) 
     { 
      doSomethingWithAllToConsole(children, tabs+"\t"); 
     } 
    } 
} 
関連する問題