Parsing XML Document using dom4j API


In this post, I will show you how to parse a XML document using dom4j API easily

Here is a sample XML file;

servers.xml

<?xml version="1.0" encoding="UTF-8"?>
<SERVERCONFIG>
<SERVERS>
    <SERVER NAME="server1" ENABLED="Y">
        <IP>10.1.2.1</IP>
        <USERNAME>admin</USERNAME>
        <PASSWORD>12345</PASSWORD>
        <DESCRIPTION>Application Server1</DESCRIPTION>
    </SERVER>
    <SERVER NAME="server2" ENABLED="Y">
        <IP>10.1.2.2</IP>
        <USERNAME>admin</USERNAME>
        <PASSWORD>54321</PASSWORD>
        <DESCRIPTION>Application Server1</DESCRIPTION>
    </SERVER>
    <SERVER NAME="server3" ENABLED="N">
        <IP>10.1.2.3</IP>
        <USERNAME>admin</USERNAME>
        <PASSWORD>012345</PASSWORD>
        <DESCRIPTION>Application Server3</DESCRIPTION>
    </SERVER>
    <SERVER NAME="server4" ENABLED="Y">
        <IP>10.1.2.4</IP>
        <USERNAME>admin</USERNAME>
        <PASSWORD>0123456</PASSWORD>
        <DESCRIPTION>Application Server4</DESCRIPTION>
    </SERVER>
    <SERVER NAME="server5" ENABLED="N">
        <IP>10.1.2.5</IP>
        <USERNAME>admin</USERNAME>
        <PASSWORD>01234567</PASSWORD>
        <DESCRIPTION>Application Server5</DESCRIPTION>
    </SERVER>
</SERVERS>
</SERVERCONFIG>

Main.java class for parsing XML file;

package xmlparsingexample;

import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
*
* @author tunatore
*/
public class Main {

public static void main(String[] args) throws DocumentException {

SAXReader reader = new SAXReader();
//loading the XML document from a file
Document document = reader.read(new File(
Main.class.getResource("servers.xml").getFile().replace("%20", " ")));
Element SERVERCONFIG = document.getRootElement();

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

for (Iterator i1 = SERVERCONFIG.elementIterator("SERVERS"); i1.hasNext();) {
Element SERVERS = (Element) i1.next();
for (Iterator i2 = SERVERS.elementIterator("SERVER"); i2.hasNext();) {
Element SERVER = (Element) i2.next();
if(SERVER.attributeValue("ENABLED").equals("Y")) {
System.out.println("------------------------------------------------");
System.out.println("SERVER NAME--> " + SERVER.attributeValue("NAME"));
System.out.println("IP--> " + SERVER.elementText("IP"));
System.out.println("USERNAME--> " + SERVER.elementText("USERNAME"));
System.out.println("PASSW0RD--> " + SERVER.elementText("PASSWORD"));
System.out.println("DESCRIPTION--> " + SERVER.elementText("DESCRIPTION"));
System.out.println("SERVER NAME--> " + SERVER.attributeValue("NAME"));
System.out.println("------------------------------------------------");
}
}
}
System.out.println("SERVERS.........................................");
}
}

Output of the program; (Only enable=”Y” servers in the XML file will be listed)

 run:
SERVERS…………………………………..
————————————————
SERVER NAME–> server1
IP–> 10.1.2.1
USERNAME–> admin
PASSW0RD–> 12345
DESCRIPTION–> Application Server1
SERVER NAME–> server1
————————————————
————————————————
SERVER NAME–> server2
IP–> 10.1.2.2
USERNAME–> admin
PASSW0RD–> 54321
DESCRIPTION–> Application Server1
SERVER NAME–> server2
————————————————
————————————————
SERVER NAME–> server4
IP–> 10.1.2.4
USERNAME–> admin
PASSW0RD–> 0123456
DESCRIPTION–> Application Server4
SERVER NAME–> server4
————————————————
SERVERS…………………………………..
BUILD SUCCESSFUL (total time: 0 seconds)

Download Source