Hello everyone.

I am trying to get a hang of XML parsing and can't get this to show me the 
right elements as I'm fairly new to Android/Java dev. Would appreciate 
some guidance.

I've build a class that gets the custom xml url and needs to parse it and 
fill ArrayList with values. After trying several versions I can't wrap my 
head around parsing nodes and extracting element values.

This is what I have

  DocumentBuilderFactory factory = DocumentBuilderFactory.*newInstance*();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(feedURL);

   // get xml document root element
Element root = doc.getDocumentElement();

   // get node list of all elements with tag name item
NodeList items = root.getElementsByTagName("item");

 ArrayList<String[]> photoSet = *new* ArrayList<String[]>();

*for* (*int* i = 0; i < items.getLength(); i++) {

                Node itemNode = items.item(i);

                NodeList itemChildNodes = itemNode.getChildNodes();

                Node itemChildNode = itemChildNodes.item(2);

                String value = itemChildNode.getNodeValue();

                 Log.*d*("ITEM ELEMENT VALUE", "Node value for item["+i+"] 
-> "+value);

}

here's what the XML looks like:

<gallery>
<item> <photoid>509</photoid> <url>
http://a2.sphotos.ak.fbcdn.net/hphotos-ak-ash4/320622_10150298438271744_326149431743_8468654_1542995730_n.jpg
</url> <title>Foster The People DC</title> <description></description> 
<category></category> <dateupdated>2012-01-24 23:31:58</dateupdated> 
<dateupdated2>Wed Jan 25 07:31:58 GMT-0800 2012</dateupdated2>

</item>

<item> <photoid>508</photoid> <url>
http://a8.sphotos.ak.fbcdn.net/hphotos-ak-ash4/308152_10150298438346744_326149431743_8468655_312468158_n.jpg
</url> <title>Foster The People Philly</title> <description></description> 
<category></category> <dateupdated>2012-01-24 23:31:44</dateupdated> 
<dateupdated2>Wed Jan 25 07:31:44 GMT-0800 2012</dateupdated2></item>
</gallery>

So now why I put itemChildNodes.item(2) is basically because I thought that 
would give me back 3rd subchild element of that node which is in this case 
<title> tag under <item> node

Would someone be kind enough to educate me on why this is not working and 
what's the best way to do this. The reason I'm not using SAX is because 
these XML files will be fairly small so I went with DocumentBuilder and 
also I wanted to learn both ways.


Many thanks good people.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to