Friday, March 30, 2012

[android-developers] Re: XML Parsing w/ DocumentBuilder

So I did make it work but feel free to pitch in and tell me if this is efficient or proper way.. I'm getting results fine with this

try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();


Document doc = builder.parse(feedURL);

doc.getDocumentElement().normalize();


// get xml document root element

Element root = doc.getDocumentElement();


// get node list of all elements with tag name item

NodeList items = root.getElementsByTagName("Song");


ArrayList<String[]> musicSet = new ArrayList<String[]>();


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


Node itemNode = items.item(i);


if (itemNode.getNodeType() == Node.ELEMENT_NODE) {


Element itemsAsElement = (Element) itemNode;


Node _musicid=(Node) itemsAsElement.getElementsByTagName("musicid").item(0).getChildNodes().item(0);

Node _asin=(Node) itemsAsElement.getElementsByTagName("ASIN").item(0).getChildNodes().item(0);

Node _songname=(Node) itemsAsElement.getElementsByTagName("songname").item(0).getChildNodes().item(0);

Node _albumname=(Node) itemsAsElement.getElementsByTagName("albumname").item(0).getChildNodes().item(0);

Node _albumurl=(Node) itemsAsElement.getElementsByTagName("albumurl").item(0).getChildNodes().item(0);

Node _albuminfo=(Node) itemsAsElement.getElementsByTagName("albuminfo").item(0).getChildNodes().item(0);

Node _cover=(Node) itemsAsElement.getElementsByTagName("cover").item(0).getChildNodes().item(0);

Node _isfree=(Node) itemsAsElement.getElementsByTagName("isfree").item(0).getChildNodes().item(0);

Node _ringurl=(Node) itemsAsElement.getElementsByTagName("ringurl").item(0).getChildNodes().item(0);

Node _songurl=(Node) itemsAsElement.getElementsByTagName("songurl").item(0).getChildNodes().item(0);


String musicid=_musicid==null ? "N/A" : _musicid.getNodeValue();

String asin=_asin==null ? "N/A" : _asin.getNodeValue();

String songname=_songname==null ? "N/A" : _songname.getNodeValue();

String albumname=_albumname==null ? "N/A" : _albumname.getNodeValue();

String albumurl=_albumurl==null ? "N/A" : _albumurl.getNodeValue();

String albuminfo=_albuminfo==null ? "N/A" : _albuminfo.getNodeValue();

String cover=_cover==null ? "N/A" : _cover.getNodeValue();

String isfree=_isfree==null ? "N/A" : _isfree.getNodeValue();

String ringurl=_ringurl==null ? "N/A" : _ringurl.getNodeValue();

String songurl=_songurl==null ? "N/A" : _songurl.getNodeValue();


itemData = new String[] { musicid, asin, songname, albumname, albumurl, albuminfo, cover, isfree, ringurl, songurl };

musicSet.add(itemData);


}

}



return musicSet;




On Thursday, March 29, 2012 10:04:44 AM UTC-7, Bozzified wrote:
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment