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

Read And Write XML Using Java


Share
 
 

Useful Java Links


       


What is XML?

 

XML stands for eXtensible Markup Language. XML is a very useful technology for describing structured information. XML was designed to transport and store data. An XML file is used for  describing a program configuration. XML was designed as a simplified version of SGML for use on the Internet.

 

Read XML in Java

 

To process an XML document, we need to parse it. A parser is a program that reads a file, confirms that the file has the correct format, breaks it up into the constituent elements, and lets a programmer access those elements.

 

There are two kinds of XML parsers:

 

  • The Document Object Model (DOM) parser reads an XML document into a     tree structure.
  • The Simple API for XML (SAX) parser generates events as it reads an XML document.

 

Here in this article I am explaining how to read XML from a DOM parser.

 

            To read an XML document, we need a DocumentBuilder object which we get from a DocumentBuilderFactory, like this:

 

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder  = factory.newDocumentBuilder();

 

Now we can read a document from file . like this:

 

                         File = new File("Employee.xml");

                        Document doc = builder.parse(f);

 

We can also use a URL to read the XML document

 

                        URL u = http://…./book.xml

                        Document doc = builder.parse(u);

 

Or we can specify the  a input stream like this:

 

                        InputStream in = . . …..

                        Document doc = builder.parse(in);

 

The Document object is an in-memory representation of the tree structure of the XML document. It is composed of objects whose classes implement the Node interface and its various subinterfaces.

  

 

Write XML in Java

 

We could write an XML file simply by making a sequence of print calls, printing the elements, attributes, and text content.

Better approach is to build up a DOM tree with the contents of the document, and then write out the tree contents. To build a DOM tree, we have to start out with an empty document. We can get an empty document by calling the newDocument method of the DocumentBuilder class.

 

            Document doc = builder.newDocument();

 

Use the createElement method of the Document class to construct the elements of our document.

 

            Element rootElement = doc.createElement(rootName);

            Element childElement = doc.createElement(childName);

 

Use the createTextNode method to construct text nodes:

 

            Text textNode = doc.createTextNode(textContents);

 

Add the root element to the document, and add the child nodes to their parents:-

 

            doc.appendChild(rootElement);

            rootElement.appendChild(childElement);

            childElement.appendChild(textNode);

 

We can set the attribute of the element using setAttribute method of the Element class. like this:-

            rootElement.setAttribute(name, value);

 

DOM API currently has no support for writing a DOM tree to the  output stream. To overcome this limitation, we will use the XML Style Sheet Transformations (XSLT) API  like this:-

 

            Transformer t = TransformerFactory.newInstance().newTransformer();

            // set output properties to get a DOCTYPE node

            t.setOutputProperty("doctype-system",systemIdentifier);

            t.setOutputProperty("doctype-public",publicIdentifier);

            // apply the "do nothing" transformation

            // and send the output to a file

            t.transform(new DOMSource(doc),

            new StreamResult(new FileOutputStream(file)));

 

For More Tutorials  & Complete source code on Read-Write XML File in Java, please refer:

 

Java program to read xml using DOM parser

 

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 read-write-xml-using-java-tutorial, 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 read-write-xml-using-java-tutorial, to receive updates on read-write-xml-using-java-tutorial,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: read-write-xml-using-java-tutorial tutorial,read-write-xml-using-java-tutorial in java,Concepts of read-write-xml-using-java-tutorial,Java,J2EE,Interview Questions,read-write-xml-using-java-tutorial 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