2013-04-05 93 views
0

我試圖解析這個XML在Java中:XML解析。我怎樣才能得到孩子的孩子?

<entities> 
<entity name="product_section" id="1"> 
    <product_type>3</product_type> 
    <section_type>1</section_type> 
    <name>Empresa</name> 
    <description>d</description> 
    <position>1</position> 
    <align>left</align> 

    <files section_id="1"> 
     <ico id="ico_1" type="normal" src="sections/1/icons/ico.png"></ico> 
     <ico id="ico_2" type="hover" src="sections/1/icons/ico.png"></ico> 
     <ico id="ico_3" type="active" src="sections/1/icons/ico.png"></ico> 

     <img id="img_1" type="normal" src="sections/1/img/pestanya.png"></img> 
     <img id="img_2" type="hover" src="sections/1/img/pestanya-hover.png"></img> 
     <img id="img_3" type="active" src="sections/1/img/pestanya-active.png"></img> 

     <background id="background_1" type="background" position="1" src="sections/1/background/bg1.png"></background> 
     <background id="background_2" type="background" position="2" src="sections/1/background/bg2.png"></background> 
     <background id="background_3" type="background" position="3" src="sections/1/background/bg3.png"></background> 
    </files> 
</entity> 

但我就是通過實體實現的循環,讓所有實體,並<product_type><section_type>等 但我想循環通過文件。

這是我的執行至今:

try { 
     File contingut = new File("xmlfile.xml"); 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(contingut); 
     doc.getDocumentElement().normalize(); 
     System.out.println("root of xml file " + doc.getDocumentElement().getNodeName()); 
     //loop a cada entity 
     NodeList nodes = doc.getElementsByTagName("entity"); 
     for (int i = 0; i < nodes.getLength(); i++) { 
      Node node = nodes.item(i); 
      Element element = (Element) node; 
      System.out.println("product_type: " + getValue("product_type", element)); 
      System.out.println("section_type: " + getValue("section_type", element)); 
      System.out.println("name: " + getValue("name", element)); 
      System.out.println("description: " + getValue("description", element)); 
      System.out.println("position: " + getValue("position", element)); 
      System.out.println("align: " + getValue("align", element)); 
     } 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

getValue功能是:

private static String getValue(String tag, Element element) { 
    NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes(); 
    Node node = (Node) nodes.item(0); 
    return node.getNodeValue(); 
} 

我已經做了很多的谷歌搜索的,而我看到的是「簡單」的例子,有父母和孩子,但不是孩子的孩子。

任何幫助,將不勝感激。

+0

那麼是什麼問題?對於文件標記,您需要編寫另一個getValue函數 – 2013-04-05 09:33:39

+1

您並未使用android,但在android開發頁面上,有一個非常簡潔的pullparser工作原理示例。也許值得檢查一下,因爲它在普通的java中是一樣的。它在這裏http://developer.android.com/training/basics/network-ops/xml.html – WereWolfBoy 2013-04-05 09:35:37

回答

1

在第一個建議:

if (element.getNodeType() == Element.ELEMENT_NODE) { // do smth} 

,並回答你的問題:

您可以

Element element = (Element) node;

使用此代碼或這樣的事情後

檢查元素類型只需重寫你的代碼。在你創建element後,你可以使用element.getChildNodes();

它可以得到所有的子標籤。之後,你寫簡單的循環,你從節點列表中得到每個節點元素,如下所示:

NodeList nodes = element.getChildNodes(); 
for(int i =0; i < nodes.getLength(); i++){ 
    Element child = (Element) nodes.item(i); 
    if(child.getNodeType() == Element.ELEMENT_NODE){ 
      String tagName = child.getTagName(); 
      if(!tagName.equals("files")){ 
        System.out.println(tagName + " : " + child.getTextContent()); 
      }else{ 
        NodeList filesChilds = child.getChildNodes(); 
        for(int j = 0; j < filesChilds.getLength(); j++){ 
         //and like above 
        } 
      } 
    } 
} 
相關問題