Moving ahead from Android XML Parsing using SAXParser, here we are going to see about how to parse a XML using DOM Parser.
we are going to parse XML from net ( by passing URL ) not from local file or string.
we are going to parse XML from net ( by passing URL ) not from local file or string.
The output looks similar to
[sourcecode language="xml"]
<maintag>
<item>
<name>AndroidPeople</name>
<website category="android">www.androidpeople.com</website>
</item>
<item>
<name>iPhoneAppDeveloper</name>
<website category="iPhone">www.iphone-app-developer.com</website>
</item>
</maintag>
[/sourcecode]
<maintag>
<item>
<name>AndroidPeople</name>
<website category="android">www.androidpeople.com</website>
</item>
<item>
<name>iPhoneAppDeveloper</name>
<website category="iPhone">www.iphone-app-developer.com</website>
</item>
</maintag>
[/sourcecode]
XMLParsingDOMExample.java
This is main activity class. when App. starts this file will be called first.
This file contains how to use DOM Parser to handle XML tags.
This file contains how to use DOM Parser to handle XML tags.
[sourcecode language="java"]
package com.androidpeople.xml.parsing;
package com.androidpeople.xml.parsing;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class XMLParsingDOMExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
TextView name[];
TextView website[];
TextView category[];
try {
URL url = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
website[i] = new TextView(this);
category[i] = new TextView(this);
website[i] = new TextView(this);
category[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("name");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("Name = "
+ ((Node) nameList.item(0)).getNodeValue());
NodeList nameList = fstElmnt.getElementsByTagName("name");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("Name = "
+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("website");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
website[i].setText("Website = "
+ ((Node) websiteList.item(0)).getNodeValue());
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
website[i].setText("Website = "
+ ((Node) websiteList.item(0)).getNodeValue());
category[i].setText("Website Category = "
+ websiteElement.getAttribute("category"));
+ websiteElement.getAttribute("category"));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
setContentView(layout);
}
}
}
[/sourcecode]
The content of your blog is exactly what I needed, I like your blog, I sincerely hope that your blog good and very useful for us.
ReplyDeleteEcommerce developer
nice blog and can u put zip file for this.....
ReplyDeletenice blog..i also love take a step to help other
ReplyDeletehttp://androidtrainningcenter.blogspot.in/
i m not able to get output for the above code.. got some exception in android.os.NetworkOnMainThreadException and gralloc_goldfish error (i.e) Emulator without GPU emulation detected .... help me for this problem. i gng this program for 2 days
Deleteto avoid this exception by using strictMode.setThreadPolicy(policy);
DeleteNice Blog...Very nice ..Keep it up
ReplyDeletethnq........................
ReplyDeletecan u tell how the same result can be displayed in listview?
ReplyDeletevry nice blog....
ReplyDeleteWTF..! This the same tutorial as in Android People, this is a great example for Spamming & Copyright infringement
ReplyDeletei m not able to get output for the above code.. got some exception in android.os.NetworkOnMainThreadException and gralloc_goldfish error (i.e) Emulator without GPU emulation detected .... help me for this problem. i gng this program for 2 days
ReplyDeletevery useful,thanx....
ReplyDeleteYou made some decent points there. I looked on the internet for the issue and found most individuals will go along with with your website.
ReplyDeleteHello I took this code but at start that doesn't occur. There is an empty appendix. Please answer in what there can be a problem.
ReplyDeleteI m getting the same problem
ReplyDeleteSolved the problem... ADD USES permission INTERNET to ur application......... JIBOOMBA u ll c d output..
ReplyDeleteNice post.Give it up. Thanks for share this article. For more visit:Ecommerce Website Design and Development Company In Bangalore|Magento Ecommerce website Development company
ReplyDeleteVery useful tutorial
ReplyDeleteEcommerce Company
Ecommerce Website Development
Ecommerce Development Company