Parsing / Reading XML file in Java.

java-xml-dukeWhile working on a small requirement, I had to write a small piece of code that can parse an XML file in Java. Although there are number of libraries available in Java which does this task efficiently, I ended up in using normal Java XML parsing using org.w3c.dom parser.

You may want to copy this code and customize it to your to fit it for your need.

Following is the sample XML file that I used.

XML File

Source Code of XML Parser

Following is the source code of sample XML parser in Java that I used to parse sample XML file.

package net.viralpatel.java.xmlparser;

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 XMLParser {

	public void getAllUserNames(String fileName) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			File file = new File(fileName);
			if (file.exists()) {
				Document doc = db.parse(file);
				Element docEle = doc.getDocumentElement();

				// Print root element of the document
				System.out.println("Root element of the document: "
						+ docEle.getNodeName());

				NodeList studentList = docEle.getElementsByTagName("student");

				// Print total student elements in document
				System.out
						.println("Total students: " + studentList.getLength());

				if (studentList != null && studentList.getLength() > 0) {
					for (int i = 0; i < studentList.getLength(); i++) {

						Node node = studentList.item(i);

						if (node.getNodeType() == Node.ELEMENT_NODE) {

							System.out
									.println("=====================");

							Element e = (Element) node;
							NodeList nodeList = e.getElementsByTagName("name");
							System.out.println("Name: "
									+ nodeList.item(0).getChildNodes().item(0)
											.getNodeValue());

							nodeList = e.getElementsByTagName("grade");
							System.out.println("Grade: "
									+ nodeList.item(0).getChildNodes().item(0)
											.getNodeValue());

							nodeList = e.getElementsByTagName("age");
							System.out.println("Age: "
									+ nodeList.item(0).getChildNodes().item(0)
											.getNodeValue());
						}
					}
				} else {
					System.exit(1);
				}
			}
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	public static void main(String[] args) {
		
		XMLParser parser = new XMLParser();
		parser.getAllUserNames("c:\\test.xml");
	}
}

Following is the output of our XML parser.

Program Output

Root element of the document: students
Total students: 3
==============================
Name: John
Grade: B
Age: 12
==============================
Name: Mary
Grade: A
Age: 11
==============================
Name: Simon
Grade: A
Age: 18

You can customise this code for your input XML file.

Get our Articles via Email. Enter your email address.

You may also like...

29 Comments

  1. JLemire says:

    There is bug or bad display at line 33.
    if (studentList != null &amp;amp;amp;&amp;amp;amp; studentList.getLength() > 0) {

  2. Viral Patel says:

    Thanks JLemire, I have updated the source and removed the typo error.

  3. Ajay says:

    Hi How to read this XML file and store it into HashSet?

  4. anon says:

    You want want to look at vtd-xml, the latest and most advanced parsing/indexing/XPath engine that offers a lot of cool features

    vtd-xml

  5. Rajendra Jogas says:

    i have made one project in eclipse for reading xml file, i have write one function for that

    function readXMLFile(){
    alert(“calling”);

    alert(“DocumentBuildFactory instance”);

    alert(“Reading xml file”);

    alert(“normalize”);

    alert(nodeName);
    }

    until the normalize it was working fine but when its try to fetch node name it will shows nothing

    could anybody help me to solve it?
    and one more thing where should i put xml file to read it from java file

  6. This is what i meaned by concepting Xml parsing in Java, great stuff!

  7. srikanth kamsali says:
    //To simplify further
    
    import java.io.File;
    import org.w3c.dom.Document;
    import org.w3c.dom.*;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException; 
    
    public class XMLReader{
    
        public static void main (String argv []){
          
          String[] TagNames = {"first","last","age"}; // Enter the node name as elements in an array 
        try {
    
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                Document doc = docBuilder.parse (new File("C:\\JavaTesting\\file.xml"));// path of the xml file
                
                NodeList list = doc.getElementsByTagName("*");
                System.out.println("XML Elements: ");
                for (int i=0; i&lt;list.getLength(); i++) {
                // Get element
                Element element = (Element)list.item(i);
                System.out.println(element.getNodeName());
                }
                
                
                // normalize text representation
                doc.getDocumentElement ().normalize ();
                System.out.println (&quot;Root element of the doc is &quot; +doc.getDocumentElement().getNodeName());
    
    
                NodeList listOfPersons = doc.getElementsByTagName(&quot;person&quot;);
                int totalPersons = listOfPersons.getLength();
                System.out.println(&quot;Total no of people : &quot; + totalPersons);
                
            
              
                for(int s=0; s&lt;listOfPersons.getLength() ; s++){
    
    
                    Node firstPersonNode = listOfPersons.item(s);
                    if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
                      
                      Element firstPersonElement = (Element)firstPersonNode;
    
                      for (int i = 0; i &lt; TagNames.length; i++) {
                      
                        NodeList firstNameList = firstPersonElement.getElementsByTagName(TagNames[i]);
                        Element firstNameElement = (Element)firstNameList.item(0);
    
                        NodeList textFNList = firstNameElement.getChildNodes();
                        System.out.println(TagNames[i]+&quot; : &quot; + ((Node)textFNList.item(0)).getNodeValue().trim());
                        
                      }
                       
    
                    }//end of if clause
    
    
                }//end of for loop with s var
    
    
            }catch (SAXParseException err) {
            System.out.println (&quot;** Parsing error&quot; + &quot;, line &quot; 
                 + err.getLineNumber () + &quot;, uri &quot; + err.getSystemId ());
            System.out.println(&quot; &quot; + err.getMessage ());
    
            }catch (SAXException e) {
            Exception x = e.getException ();
            ((x == null) ? e : x).printStackTrace ();
    
            }catch (Throwable t) {
            t.printStackTrace ();
            }
            //System.exit (0);
        
        
    
        }//end of main
    
    
    }
    // Still we can have a generic xml reader with dynamic tag names pick. :)
    
    • johny isakson says:

      Perfect!!

  8. sumithpt says:

    Thanks man, It was very useful.

  9. Anand says:

    Thanks.. Very informative..

  10. Joe says:

    How do you parse this xml file.

    Ishan

    cards
    notes
    dice

    50 50 10
    offices

    Connor

    notes
    dice
    cards

    10 10 10
    security room

  11. Sridhar Prabhakar says:

    Thanks for the tutorial. After going through several blogs including yours, I wrote a utility class to handle all XML related activities. Just thought it might be helpful to anyone trying to do basic stuff with XML.

    http://sridharprabhakartechramblings.blogspot.in/2012/10/xml-parsing-made-easy.html

  12. poorni says:

    hi,
    this code is ok for reading the xml file given the particular tag name(specified). but to read xml file data which consist of dynamic tags its not possible. can u help me out in getting the tag value dynamically and reading the xml file

  13. Jc Baskaran says:

    hi
    even i wanted to read xml file dynamically. can someone help me out in this.

  14. jamil says:

    Very useful thanks .

  15. fatemeh says:

    I copy the codes into a new project in itellij
    and I get this
    org.xml.sax.SAXParseException: Content is not allowed in prolog.
    [Fatal Error] :1:1: Content is not allowed in prolog.

    Process finished with exit code 0

    I’m new to Java, may you help me?
    thanks!

  16. Neha says:

    Hi,

    I want to read multiple XML files dynamically … can anybody help ?

    Thanks

  17. Diego Del Aguila says:

    I need to create a java program wich connects to a database, reads the entire database and generate a xml with the databased mapped… any idea ?

  18. Saurabh says:

    Sir, I have a xml file from that i have to store id value , name value and days value in collection so later i can use that… can you tell me the way..?

  19. kss says:

    Sorry for type

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><HostIdentifiers>
    <HostIdentifier>
        <id>a51d065bd0e8c4a7faf1044ecbcd0ee4f</id>
        <name>IAMSuiteAgent</name>
        <description>Host identifier for IAM Suite resources</description>
        <Hosts>
            <host>
                <hostName>IAMSuiteAgent</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    
    <HostIdentifier>
        <id>a2d71567a8d2845de931818cd70fa64e7</id>
        <name>MDC_TEST_WEBGATE</name>
        <description>Host Identifier created for agent during Remote Registration</description>
        <Hosts>
            <host>
                <hostName>MDC_TEST_WEBGATE</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    
    <HostIdentifier>
        <id>a3080ff5e01914b1c844e3dfe94576918</id>
        <name>MDC</name>
        <description>Host Identifier created for agent during Remote Registration</description>
        <Hosts>
            <host>
                <hostName>MDC</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    
    <HostIdentifier>
        <id>a29aa760cf66145c68f821041ed6faf28</id>
        <name>DCC</name>
        <description>Host Identifier created for agent during Remote Registration</description>
        <Hosts>
            <host>
                <hostName>DCC</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    
    <HostIdentifier>
        <id>a7f2f5c580d8d4ac193daf5abec3a8f1e</id>
        <name>SAGAR_HOST_ID</name>
        <description>Host Identifier created BY SAGAR</description>
        <Hosts>
            <host>
                <hostName>SAGAR_HOST_ID3</hostName>
            </host>
            <host>
                <hostName>SAGAR_HOST_ID</hostName>
            </host>
            <host>
                <hostName>SAGAR_HOST_ID2</hostName>
            </host>
            <host>
                <hostName>SAGAR_HOST_ID1</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    
    <HostIdentifier>
        <id>a28cc8cb56ad9496eb07e6e4471b38025</id>
        <name>AMIT_HOST_ID</name>
        <description>Host Identifier created BY AMIT</description>
        <Hosts>
            <host>
                <hostName>AMIT_HOST_ID4</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID7</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID6</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID5</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID9</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID3</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    </HostIdentifiers> 

    and output should be

    <HostIdentifier>
        <id>a28cc8cb56ad9496eb07e6e4471b38025</id>
        <name>AMIT_HOST_ID</name>
        <description>Host Identifier created BY AMIT</description>
        <Hosts>
            <host>
                <hostName>AMIT_HOST_ID4</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID7</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID6</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID5</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID9</hostName>
            </host>
            <host>
                <hostName>AMIT_HOST_ID3</hostName>
            </host>
        </Hosts>
    </HostIdentifier>
    
     
  20. sakura says:

    Can you drive me the requirement in a question below?
    Link: http://stackoverflow.com/questions/24112813/how-to-read-xml-file-using-sax-dom-parser-in-java

  21. Jcoder says:

    File not Found!!!!!
    can u please tell me how to give input .xml file directory
    i know this is a simple question but i am new to this

  22. Sathishwaran says:

    Is there any ways to read this type of .xml file

    i want to get parameter value according to the parameter name
    If any thoughts please let me know.

  23. Ian Carl Gimenez says:

    Hi,

    I have this xml

    <channel>
    <title>PSI Update</title>
    <source>Airviro</source>
    <item>
    	<region>
    		<id>rNO</id>
    		<latitude>1.41803</latitude>
    		<longitude>103.82000</longitude>
    		<record timestamp="20150130100000">
    			<reading type="NPSI" value="27"/>
    			<reading type="NPSI_PM25_3HR" value="43"/>
    			<reading type="NO2_1HR_MAX" value="28"/>
    			<reading type="PM10_24HR" value="13"/>
    			<reading type="PM25_24HR" value="6"/>
    			<reading type="SO2_24HR" value="6"/>
    			<reading type="CO_8HR_MAX" value="0.38"/>
    			<reading type="O3_8HR_MAX" value="8"/>
    			<reading type="NPSI_CO" value="4"/>
    			<reading type="NPSI_O3" value="3"/>
    			<reading type="NPSI_PM10" value="13"/>
    			<reading type="NPSI_PM25" value="27"/>
    			<reading type="NPSI_SO2" value="4"/>
    		</record>
    	</region>
    	<region>
    		<id>NRS</id>
    		<latitude>0</latitude>
    		<longitude>0</longitude>
    		<record timestamp="20150130100000">
    			<reading type="NPSI" value="48"/>
    			<reading type="NPSI_PM25_3HR" value="41"/>
    			<reading type="NO2_1HR_MAX" value="42"/>
    			<reading type="PM10_24HR" value="21"/>
    			<reading type="PM25_24HR" value="11"/>
    			<reading type="SO2_24HR" value="24"/>
    			<reading type="CO_8HR_MAX" value="0.72"/>
    			<reading type="O3_8HR_MAX" value="19"/>
    			<reading type="NPSI_CO" value="7"/>
    			<reading type="NPSI_O3" value="8"/>
    			<reading type="NPSI_PM10" value="21"/>
    			<reading type="NPSI_PM25" value="48"/>
    			<reading type="NPSI_SO2" value="15"/>
    		</record>
    	</region>
    </item>
    </channel>
    

    From this xml, I would only want to get the type (i.e., NPSI, NPSI_PM25_3HR) and the value attribute. How can I be able to parse it in java?

    Hope you can help me on this. Thanks in advance.

  24. Annamalai says:

    Nice article

  25. Victor says:

    Thanks a lot. This piece of code has made my day.

  26. Rasika Patil says:

    Hi.. This is a fantastic Article..

    Further on this, could you please help on how to store the above output into a database(table having count of students, name, age, etc.) ? Waiting for your response !

  27. jane maurice says:

    Thoughtful ! I learned a lot from the points – Does anyone know where my assistant can access a template a form document to edit ?

  28. Rucha Bapat says:

    My XML is very unstructured. Can you help me with a code to parse the very large unstructured xml?

Leave a Reply

Your email address will not be published. Required fields are marked *