On this page you can find a simple guide to reading and writing XML files. The code examples here give you everything you need to read  and write to XML files.

a. Read XML file
b. Write XML file

a. Read XML file

This Section show you how to read  an XML file content

XML File Content(Book.xml)

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <description>An in-depth look at creating applications 
      with XML.</description>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>

Code: To read name of  Authors(Gambardella, Matthew  & Ralls, Kim) from the above XML file(Book.xml)

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXML {
    public static void main(String[] args) throws Exception  {

        //The two lines below are just for getting an
        //instance of DocumentBuilder which we use
        //for parsing XML data
        DocumentBuilderFactory factory =
        DocumentBuilder builder = factory.newDocumentBuilder();

        //Parsing of Book.xml is done here
        Document doc = builder.parse(new File("C:/Javaseleniumworld/Book.xml"));

        //Here we get the root element of XML and print out
        Element rootElement = doc.getDocumentElement();

        //Here we get a list of all elements named 'child'
        NodeList list = rootElement.getElementsByTagName("author");

        //Traversing all the elements from the list and printing
        //out its data
        for (int i = 0; i < list.getLength(); i++) {
            //Getting one node from the list.
            Node childNode = list.item(i);
            System.out.println("Name of the Author : " + childNode.getTextContent());


Name of the Author : Gambardella, Matthew
Name of the Author : Ralls, Kim

b. Write an XML file

This Section will show you how to write a fresh XML with your defined tags

Code: A XML file with fresh set of tags and value is created

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class WriteXML {
	public static void main(String argv[]) {
	  try {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("Catalog");
		// Book elements
		Element staff = doc.createElement("Book");
		// set attribute to Book element
		Attr attr = doc.createAttribute("id");
		// Author elements
		Element firstname = doc.createElement("author");
		// title elements
		Element lastname = doc.createElement("title");
		// genre elements
		Element nickname = doc.createElement("genre");
		// price elements
		Element salary = doc.createElement("price");
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("C:/Javaseleniumworld/file.xml"));
		transformer.transform(source, result);
		System.out.println("File saved!");
	  } catch (ParserConfigurationException pce) {
	  } catch (TransformerException tfe) {


XML file "file.xml" with below mentioned content is created

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
 <Book id="1">