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:-






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



            // 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

Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)