Using java to process XML - sample code

Processing XML document using the Java code is simple if you know the logic and the classes and the methods that are used in it. A document object is created to load the XML file that you want to process. The following code sample would do that,

_______________________________________________


_______________________________________________

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

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse (new File("ur_xml_file.xml"));
. . .
. . .
doc.getDocumentElement().getNodeName());
NodeList nl = doc.getElementsByTagName("element_name");
. . .
. . .

The above code could be understood if you see how the document object is created and the XML file you want to process is loaded into that document object. Once the XML document is loaded you can use the getDocumentElement().getNodeName() to get the name of the root element.

You can create a NodeList object and get all the nodes by Tagname using the method getElementsByTagName(). This method takes the name of the element that you want as the argument in the form of string. You can loop through the NodeList object using a 'for' loop and work with the elements.

An Introduction to JSP Standard Template Library (JSTL)
The JSP Standard Template Library (JSTL) is a very new component released by Sun for JSP programming. JSTL allows you to program your JSP pages using tags, rather than the scriptlet code that most JSP programmers are already accustomed to.

The Fast Track From Code to Cloud
Over the past decade, software development has undergone several transformations that have led to the new practices and methodologies such as agile development. Combining emerging technologies -- including dynamic language platforms, cloud computing, and agile development practices -- offers a paradigm shift away from traditional software development methods.



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?