Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Java program to read xml using DOM parser


Share
 
 

Useful Java Links


       


Sample program to read XML

 

Below Java code parse the book.xml file.

 

book.xml

 

<?xml version="1.0"?>

<catalog>

   <book id="bok101">

      <author>Gambardella, Matthew</author>

      <title>SCJP</title>

      <genre>Computer</genre>

      <price>495</price>

      <publish_date>2010-10-01</publish_date>

      <description>Sun certified Java programming reference .</description>

   </book>

   <book id="bok102">

      <author>Ralls, Kim</author>

      <title>XML Reference</title>

      <genre>Fantasy</genre>

      <price>595</price>

      <publish_date>2008-12-16</publish_date>

      <description>An in-depth look at creating applications

      with XML.</description>

   </book>

</catalog>

 

The Below Class XMLReader  reads the above XML File. You can copy paste the code in eclipse or net beans IDE and use it as a reference:

 

Java program to read xml using DOM parser

 

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
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;

public class XMLReader {

 public static void main(String argv[]) {

  try {
  File file = new File("c:\\book.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  NodeList nodeLst = doc.getElementsByTagName("book");
  System.out.println("Information of all books");

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
    
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
  
           Element fstElmnt = (Element) fstNode;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("author");
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
      NodeList fstNm = fstNmElmnt.getChildNodes();
      System.out.println("author : "  + ((Node) fstNm.item(0)).getNodeValue());
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("title");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("title : " + ((Node) lstNm.item(0)).getNodeValue());

      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("genre");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("genre : " + ((Node) lstNm.item(0)).getNodeValue());

      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("price");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("price : " + ((Node) lstNm.item(0)).getNodeValue());

      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("publish date");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("publish date: " + ((Node) lstNm.item(0)).getNodeValue());

      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("description");
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
      NodeList lstNm = lstNmElmnt.getChildNodes();
      System.out.println("description: " + ((Node) lstNm.item(0)).getNodeValue());
    }

  }
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
}

 

 To read about, how to write book.xml using Java, click the below link:

 

Java program to write xml using DOM parser




Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on Java-program-to-read-xml-using-DOM-parser, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on Java-program-to-read-xml-using-DOM-parser, to receive updates on Java-program-to-read-xml-using-DOM-parser,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: Java-program-to-read-xml-using-DOM-parser tutorial,Java-program-to-read-xml-using-DOM-parser in java,Concepts of Java-program-to-read-xml-using-DOM-parser,Java,J2EE,Interview Questions,Java-program-to-read-xml-using-DOM-parser Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US