Return to Snippet

Revision: 25722
at April 7, 2010 03:49 by PravinChk


Updated Code
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class DocumentBuilderDemo {

	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilder db = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		db = dbf.newDocumentBuilder();
		Document doc = db.parse(new File("c:\\simple.xml"));
		
		Element root = doc.getDocumentElement();
		System.out.println(root.getNodeName());
		
		System.out.println("========================================================================");
		
		NodeList nodeList = root.getElementsByTagName("to");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		System.out.println("========================================================================");
		
		nodeList = root.getElementsByTagName("from");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		System.out.println("========================================================================");
		
		nodeList = root.getElementsByTagName("body");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
	
		System.out.println("========================================================================");
	
		
	}

}


/*
The simple.xml file that we used for parsing in above example.


<?xml version="1.0"?>
<msg>
	<note>
		<to>Tom</to>
		<from>Prince</from>
		<heading>Reminder</heading>
		<body>Hello how are u?</body>
	</note>
	<note>
		<to>Sania</to>
		<from>Prince</from>
		<heading>Joking</heading>
		<body>Welcome!!!</body>
	</note>
</msg>

*/

Revision: 25721
at April 7, 2010 03:42 by PravinChk


Updated Code
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class DocumentBuilderDemo {

	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilder db = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		db = dbf.newDocumentBuilder();
		Document doc = db.parse(new File("c:\\simple.xml"));
		
		Element root = doc.getDocumentElement();
		System.out.println(root.getNodeName());
		
		System.out.println("========================================================================");
		
		NodeList nodeList = root.getElementsByTagName("to");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		System.out.println("========================================================================");
		
		nodeList = root.getElementsByTagName("from");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		System.out.println("========================================================================");
		
		nodeList = root.getElementsByTagName("body");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
	
		System.out.println("========================================================================");
	
		
	}

}

Revision: 25720
at April 7, 2010 03:39 by PravinChk


Initial Code
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class DocumentBuilderDemo {

	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilder db = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		db = dbf.newDocumentBuilder();
		Document doc = db.parse(new File("c:\\simple.xml"));
		
		Element root = doc.getDocumentElement();
		System.out.println(root.getNodeName());
		NodeList nodeList = root.getElementsByTagName("to");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		nodeList = root.getElementsByTagName("from");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		nodeList = root.getElementsByTagName("body");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		NodeList all = root.getChildNodes();
		System.out.println(all.getLength());
		
		Node n = all.item(0);
		System.out.println(n.getFirstChild());
		
	}

}

Initial URL
DocumentBuilderDemo.java

Initial Description
Code is to demonstrate the use of DocumentBuilder to parse the XML file. And retrieve the data from it.

Initial Title
DOM Parser demo using DocumentBuilder.

Initial Tags
DOM, xml

Initial Language
Java