Sample java code on how to read XML file

There are APIs for reading an XML file and displaying them. There are certain packages that you have to import before you do such a program. The following is a simple listing template that gives you information on how to create a document object and then load the XML file in it to process it.

_______________________________________________


_______________________________________________

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

DocumentBuilderFactory dbfact = DocumentBuilderFactory.newInstance();
DocumentBuilder dbuild = dbfact.newDocumentBuilder();
Document dc = dbuild.parse (new File("xml_file.xml"));
. . .
. . .
doc.getDocumentElement().getNodeName());
NodeList nlist = dc.getElementsByTagName("ele_name");
. . .
. . .

The import statements on the top gives you access to the classes and the methods that are used in reading an XML file. To create a document object a DocumentBuilderFactory object is first created and then a DocumentBuilder object is created.

With this DocumentBuilder you can create a Document object and load it with the XML file you need to process. Then a node list is created and the elements are loaded in it. This node list is looped by using a 'for' loop and worked upon.

Linked by Thom Holwerda on Thu 6th Oct 2005 11:05 UTC (New Mobile Computing)
The Servlet-Based Content Creation Framework is an easy-used and easily-understood compact, content creation framework utilizing hamlets; a servlet extension that reads XHTML and uses the simple API for XML. Read this article on introducing Hamlets and learn how they can be used to separate Java and HTML in the same source file.

George Ou compares Microsoft Office 2007 to OpenOffice 2.2 (New Mobile Computing)
in memory and CPU usage using the OOXML and ODF file formats. The conclusion according to Ou: "We can see that the OpenOffice.org ODF XML parser (while vastly improved) is still about 5 times slower than Microsoft's OOXML parser.



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