2012-07-05 2 views
0

나는 이런 종류의 질문이 이전에 여기에 제기되었지만 여전히 내 문제에 대한 해결책을 찾을 수 없다는 것을 알고있다.DOM을 사용하여 복잡한 XML을 처리하기

내가 DOM 파서를 사용하여 다음 XML 응답을 구문 분석하고 : 그래서, 사람이

상황 ..... 그것으로 나를 도울 수 바랍니다. 위의 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> 

, 내가 나는 여러 난 문제없이 구문 분석 할 수 있어요 "공급", 그러나 여기 을 받고 각각 "공급" "코멘트"없음 또는 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 
    } 
} 
관련 문제