Tuesday, July 7, 2009

How to get the attribute of elements of an XML using DOM Parsing

In my earlier article, I have given you the code how to parse the XML file to extract the data onto it. It shows you how to extract the element name and their values.

This article enlightens you how to extract the attribute of an element, if any, which I missed out earlier.


However, we are using classes of the org.w3c.dom package which comes with JDK1.6 only. We are going to use getAttributes() method of Node class which will return a object of NamedNodeMap class.


The below program will list down all the attributes’ name and values.

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMXMLParser {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document xmlDoc;
Node node = null;
DOMXMLParser() {

}

private void getXMLDoc() {
try {
db = dbf.newDocumentBuilder();
xmlDoc = db.parse(new File("Company.xml")); // Input XML
} catch (ParserConfigurationException pce) {
System.out.println("ParseConfigurationException Encountered While Building The XML Document");
pce.printStackTrace();
} catch (IOException ioe) {
System.out.println("IO Excpetion Encountered While Reading In XML File !!!");
ioe.printStackTrace();
} catch (SAXException se) {
System.out.println("SAX/Parse Exception Encountered While Parsing XML File !!!");
se.printStackTrace();
} catch (IllegalArgumentException iae) {
System.out.println("IllegalArgumentException !!! Check If File Passed Is Null ... ");
iae.printStackTrace();
}
}

private void parseXMLFile() {
System.out.println("Root Element: " + xmlDoc.getDocumentElement().getNodeName());
System.out.println("Root Element Value: " + xmlDoc.getDocumentElement().getNodeValue());
NodeList nl = xmlDoc.getChildNodes(); //get the nodelist containing all the nodes
for (int ctr = 0; ctr < nl.getLength(); ctr++) {
getSystemComponents(nl.item(ctr)); // item method is used to get the node out of nodelist
}
}



private void getSystemComponents(Node n) {
System.out.println("Current Element: " + n.getNodeName());
if (n.hasChildNodes()) {
System.out.println("Has Child Nodes");
NodeList nodeList = n.getChildNodes();
for (int ctr = 0; ctr < nodeList.getLength(); ctr ++) {
getSystemComponents(nodeList.item(ctr)); //Recursive call to get the details of a node
}
}
// Code to extract the attributes of a node, if any
NamedNodeMap attributes = (NamedNodeMap)n.getAttributes();
if(attributes!=null){
for (int g = 0; g < attributes.getLength(); g++) {
Attr attribute = (Attr)attributes.item(g);
System.out.println(" Attribute: " + attribute.getName() + " with value " +attribute.getValue());
}
}
}



public static void main(String args[]) {
DOMXMLParser parser = new DOMXMLParser();
parser.getXMLDoc();
parser.parseXMLFile();
}
}

No comments:

Total Pageviews