PARSING A COMPLEX XML USING DOM

donjakarta

New Member
I know that this sort of question has been asked here before, but still i couldn't find any solution to my problem. So please, can anyone help me with it.....Situation: I am parsing the following xml response using DOM parser.\[code\]<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>\[/code\]In the above xml response, i am getting multiple "feed", which i am able to parse without any problem, but here each "feed" can have None or N numbers of "comment". I am not able to parse the comment for an individual feed. Can anyone suggest me how do proceed in the right direction.I am also putting a snippet of code here, NOT the entire code.. that i am using to parse the xml doc, so it will be easier to pin point the problem.\[code\]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){} }\[/code\]
 
Back
Top