Java DOM Parser – Parse XML Document | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link


File : ReadXMLFile.java

package com.codeFactory;

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

	public static void main(String[] args) {
		try {
			File folder = new File("./src/com/codeFactory");
			File[] listOfFiles = folder.listFiles(); // get all files details

			for (int i = 0; i < listOfFiles.length; i++) {
				//check file is xml or not
				if (listOfFiles[i].isFile() && listOfFiles[i].toString().contains(".xml")) {
					System.out.println("\n*** File " + listOfFiles[i].getName() + " ***\n");

					File fXmlFile = new File("./src/com/codeFactory/" + listOfFiles[i].getName());
					DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
					DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
					Document doc = dBuilder.parse(fXmlFile);
					doc.getDocumentElement().normalize();
					System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
					
					// get details of 'staff' node from the file
					NodeList nList = doc.getElementsByTagName("employee");

					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());
						}
					}
				} else if (listOfFiles[i].isDirectory()) {
					System.out.println("Directory " + listOfFiles[i].getName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

File : staff.xml

<company>
    <staff id="1001">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
    <employee id="2005">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </employee>
    <programmer id="2006">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </programmer>
</company>

File : staff1.xml

<company1>
    <staff id="10010">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff1 id="20010">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff1>
    <employee id="2021">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </employee>
    <programmer id="2301">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </programmer>
</company1>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s