Example of using Java for XML parsing

Parsing of an XML document can be easily done using Java when you load the XML document in the document object. So you have to create a document object and then load that object with the XML document that you want to parse. To work with the document object you have to include certain classes in your program and this is achieved by importing some packages as given below.

_______________________________________________


_______________________________________________

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

The next three lines of code would give you an idea on how to create the document object using Java.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse (new File("ur_xml_file.xml"));

Upon interpreting the above code you can find that an DocumentBuilderFactory object is created first and from that a DocumentBuilder is created. The DocumentBuilder is used to parse the XML file and load the XML file into the document object.

Once you have the XML file loaded in the document object, you can easily work with the XML file according to your needs. There are methods like, getDocumentElement().getNodeName(),getElementsByTagName() that can be used to work with the XML document.

Recent Original Stories (New Mobile Computing)
Java Platform, Enterprise Edition (Java EE) is not going to survive as a major standard programming model in the next five years, predicts Richard Monson-Haefel, senior analyst with the Burton Group, and SOA is part of the reason.

360Works Releases ScriptMaster 2.0 at DevCon 2008 (Centre Daily Times)
360Works, a leading FileMaker product developer, is releasing a new version of its ScriptMaster tool at the 2008 FileMaker Developers Conference in Phoenix, AZ later this month. A completely free, modular plugin for FileMaker, ScriptMaster allows developers to use Java code even without Java expertise. Version 2.0 includes modules for file manipulation, URL downloads, XML Web Services, encoding ...

Aetna, Citi, Deutsche Telekom AG and UNINETT Win 2008 Liberty Alliance IDDY Awards (PR Newswire via Yahoo! Finance)
Liberty Alliance, the global identity community working to build a more trust-worthy internet for consumers, governments and businesses worldwide, today announced Aetna, Citi, Deutsche Telekom AG and UNINETT have won the 2008 IDDY Award.



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.

_______________________________________

Recommended XML Books

cover
cover
cover
cover
cover
cover



 

 

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!


New XML Books Titles - Best Seller's @ Amazon.com

Visit XML Training Material Guide Homepage

Technology helps engage students - how do classroom instruction and social dynamics change when the classroom is completely dependant on modern technology?

Dallas Web Design- local Dallas web design firm offers site design and backend system development