Monday, May 11, 2015

How to read XML file in java.

We can read a xml by following stpes-

Step 1- Create File object of xml file.

1
fXmlFile = new File("src\\myXml.xml");

Step 2- Create a document and parse the xml and sotore it into document object

1
2
3
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);



Step 3- Read a root element

1
doc.getDocumentElement().getNodeName()

Step 4- Find a node list

1
NodeList nList = doc.getElementsByTagName("staff"); 

Step 5- Make loop over this NodeList and read all the node one by one

1
2
eElement.getAttribute("id");
eElement.getElementsByTagName("firstname").item(0).getTextContent();

Example:

myXml.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0"?>
<company>
    <staff id="1001">
  <firstname>Ram</firstname>
  <lastname>Singh</lastname>
  <nickname>ram</nickname>
  <salary>100000</salary>
    </staff>
   <staff id="2001">
        <firstname>Shyam</firstname>
        <lastname>Singh</lastname>
        <nickname>shyam</nickname>
        <salary>200000</salary>
    </staff>
</company>

ReadXMLFile.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com. atozjavatutorials;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {
    public static void main(String argv[]) {
        try {
            File fXmlFile = new File("src\\myXml.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("staff");
            System.out.println("----------------------------");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Staff id : " + eElement.getAttribute("id"));
                    System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                    System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                    System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                    System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Output:

Root element :company
----------------------------
Current Element :staff
Staff id : 1001
First Name : Ram
Last Name : Singh
Nick Name : ram
Salary : 100000
Current Element :staff
Staff id : 2001
First Name : Shyam
Last Name : Singh
Nick Name : shyam
Salary : 200000

No comments :

Post a Comment