2012-07-05 104 views
0

我知道这类问题已在此处提出,但仍然找不到解决我的问题的方法。所以,请谁能帮我一下吧.....使用DOM解析复杂的XML

现状:

我解析使用DOM解析器以下XML响应。

<feed> 
<post_id>16</post_id> 
<user_id>68</user_id> 
<restaurant_id>5</restaurant_id> 
<dish_id>7</dish_id> 
<post_img_id>14</post_img_id> 
<rezing_post_id></rezing_post_id> 
<price>8.30</price> 
<review>very bad</review> 
<rating>4.0</rating> 
<latitude>22.299999000000</latitude> 
<longitude>73.199997000000</longitude> 
<near> Gujarat</near> 
<posted>1340869702</posted> 
<display_name>username</display_name> 
<username>vivek</username> 
<first_name>vivek</first_name> 
<last_name>mitra</last_name> 
<dish_name>Hash brows</dish_name> 
<restaurant_name>Waffle House</restaurant_name> 
<post_img>https://img1.yumzing.com/1000/9cab8fc91</post_img> 
<post_comment_count>0</post_comment_count> 
<post_like_count>0</post_like_count> 
<post_rezing_count>0</post_rezing_count> 
<comments> 
<comment/> 
</comments> 
</feed> 

<feed> 
<post_id>8</post_id> 
<user_id>13</user_id> 
<restaurant_id>5</restaurant_id> 
<dish_id>6</dish_id> 
<post_img_id>7</post_img_id> 
<rezing_post_id></rezing_post_id> 
<price>3.50</price> 
<review>This is cheesy!</review> 
<rating>4.0</rating> 
<latitude>42.187000000000</latitude> 
<longitude>-88.346497000000</longitude> 
<near>Lake in the Hills IL</near> 
<posted>1340333509</posted> 
<display_name>username</display_name> 
<username>Nullivex</username> 
<first_name>Bryan</first_name> 
<last_name>Tong</last_name> 
<dish_name>Hash Brows with Cheese</dish_name> 
<restaurant_name>Waffle House</restaurant_name> 
<post_img>https://img1.yumzing.com/1000/78e5c184fd3ae752f8665636381a8f0006762dc0</post_img> 
<post_comment_count>6</post_comment_count> 
<post_like_count>1</post_like_count> 
<post_rezing_count>1</post_rezing_count> 
<comments> 
<comment> 
<user_id>16</user_id> 
<email>[email protected]</email> 
<email_new></email_new> 
<email_verification_code></email_verification_code> 
<password>9d99ef4f72f9d2df968a75e058c78245fa45e9e7</password> 
<password_reset_code></password_reset_code> 
<salt>31a988badccd35a1be7dacc073f60f52e49ff881</salt> 
<username>existentialism27</username> 
<first_name>Daniel</first_name> 
<last_name>Amaya</last_name> 
<display_name>username</display_name> 
<birth_month>10</birth_month> 
<birth_day>5</birth_day> 
<birth_year>1985</birth_year> 
<city>Colorado Springs</city> 
<state>CO</state> 
<country>US</country> 
<timezone>US/Mountain</timezone> 
<last_seen>1338365509</last_seen> 
<is_confirmed>1</is_confirmed> 
<is_active>1</is_active> 
<post_comment_id>9</post_comment_id> 
<post_id>8</post_id> 
<comment>this is a test comment!</comment> 
<posted>1340334121</posted> 
</comment> 

<comment> 
<user_id>16</user_id> 
<email>[email protected]</email> 
<email_new></email_new> 
<email_verification_code></email_verification_code> 
<password>9d99ef4f72f9d2df968a75e058c78245fa45e9e7</password> 
<password_reset_code></password_reset_code> 
<salt>31a988badccd35a1be7dacc073f60f52e49ff881</salt> 
<username>existentialism27</username> 
<first_name>Daniel</first_name> 
<last_name>Amaya</last_name> 
<display_name>username</display_name> 
<birth_month>10</birth_month> 
<birth_day>5</birth_day> 
<birth_year>1985</birth_year> 
<city>Colorado Springs</city> 
<state>CO</state> 
<country>US</country> 
<timezone>US/Mountain</timezone> 
<last_seen>1338365509</last_seen> 
<is_confirmed>1</is_confirmed> 
<is_active>1</is_active> 
<post_comment_id>10</post_comment_id> 
<post_id>8</post_id> 
<comment>this is a test comment!</comment> 
<posted>1340334128</posted> 
</comment> 
</comments> 
</feed> 

在上面的XML响应,我是越来越多“饲料”,这我能够没有任何问题解析,但这里每一个“喂”可以有“注释”中没有或N号。我无法解析个别饲料的评论。任何人都可以建议我怎么做正确的方向。

我是也放在这里的代码片段,而不是整个代码..我用来解析XML文档,所以它会更容易指出问题。

DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder odb = odbf.newDocumentBuilder(); 
InputSource is = new InputSource(new StringReader(xml)); 
Document odoc = odb.parse(is); 
odoc.getDocumentElement().normalize();  
NodeList LOP = odoc.getElementsByTagName("feed"); 
System.out.println(LOP.getLength()); 

    for (int s=0 ; s<LOP.getLength() ; s++){ 

     Node FPN =LOP.item(s); 
     try{ 
      if(FPN.getNodeType() == Node.ELEMENT_NODE) 
       { 

      Element token = (Element)FPN; 

      NodeList oNameList0 = token.getElementsByTagName("post_id"); 
      Element ZeroNameElement = (Element)oNameList0.item(0); 
      NodeList textNList0 = ZeroNameElement.getChildNodes(); 

feed_post_id = Integer.parseInt(((Node)textNList0.item(0)).getNodeValue().trim()); 

System.out.println("#####The Parsed data#####"); 
System.out.println("post_id : " + ((Node)textNList0.item(0)).getNodeValue().trim()); 
System.out.println("#####The Parsed data#####"); 

      } 
      }catch(Exception ex){} 
      } 

回答

1

一旦你的饲料节点列表上,并使用运行:

NodeList nodes = feedNode.getChildNodes(); 

    for (Node node: nodes) 
    { 
    if(node.getNodeName().equals("comments")){ 
    //do something with comments node 
    } 
}