How to use Java for reading XML data

Using Java API for XML Processing (JAXP) is the easiest way to work with the XML files using Java. By using JAXP you can do anything with the XML document. You can read it, change it and even remove the elements in it. You can download the Java API for XML Processing from the Sun Microsystems website.


_______________________________________________

Once you have installed that JAXP library you can include the necessary classes in your Java program to gain access to the methods available for manipulating the XML documents.

A typical import statement in your Java program might include,

import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;

After the import is done you have to create a document object to load the XML document that you want to work with. This document object is created in three steps. First, you have to create a DocumentBuilderFactory object and from that you have to create a DocumentBuilder and from that you can create the Document object. A sample line of code that would load the XML document might look like,

Document doc = docbuilder.parse(new File("YourXmlFile.xml"));

You can use methods like getElementsByTagName() to get all the elements that have a particular name and use a 'for' loop or any other conditional loops to navigate through them and manipulate them.




_______________________________________________

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________




 

 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

Copyright - © 2004 - 2017 - All Rights Reserved.