How to Read XML file using Java without any Extra Libs ?

How to Read XML file using Java without any Extra Libs ?: XML file can be read using jdom parser, but here we are going to read xml file only with the javax xml parsers and w3c dom components.

 

Steps in overview:

  1. DocumentBuilder should be initialized with newDocumentBuilder() of builderfactory class.
  2. New Document created with document builder.
  3. Using getElementsByTagName() root tag given and retrieved all the child tags in the w3c nodelist.
  4. All the node lists are iterated and each element printed using getTextContent() method.

 

 

Sample XML Input File:

How to Read XML file using Java without any Extra Libs ?

Complete samplexml.xml file is attached at the last of this post.

 

 

Read XML File – Java Source Code:

Change the path of the xml file(samplexml.xml) and run the below program.

 

[java]

package in.javadomain;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadXmlFile {

/**
* @param args
*/
public static void main(String[] args) {

System.out.println(“XML File Reading Started”);
File xmlFile = new File(“E:/samplexmlfile.xml”);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder;
String xmlCode, xmlMerchant, xmlTitle, xmlVoucher, xmlTrackingURL, xmlExpiry, xmlActivationDate;
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document;
try {
document = documentBuilder.parse(xmlFile);
document.getDocumentElement().normalize();
NodeList nodeLst = document.getElementsByTagName(“Item”);
for (int temp = 0; temp < nodeLst.getLength(); temp++) {
Node nNode = nodeLst.item(temp);
System.out.println(“Item Tag Started”);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) nNode;
xmlCode = element.getElementsByTagName(“Code”).item(0)
.getTextContent().trim();
System.out.println(“Code Tag in Xml ==>” + xmlCode);
xmlMerchant = element.getElementsByTagName(“Merchant”)
.item(0).getTextContent().trim();
System.out.println(“Merchant Tag in Xml ==>”
+ xmlMerchant);
xmlTitle = element.getElementsByTagName(“Title”)
.item(0).getTextContent().trim();
System.out.println(“Title Tag in Xml ==>” + xmlTitle);
xmlVoucher = element
.getElementsByTagName(“VoucherCodeId”).item(0)
.getTextContent().trim();
System.out.println(“VoucherCodeId Tag in Xml ==>”
+ xmlVoucher);
xmlTrackingURL = element
.getElementsByTagName(“TrackingUrl”).item(0)
.getTextContent().trim();
System.out.println(“TrackingUrl Tag in Xml ==>”
+ xmlTrackingURL);
xmlExpiry = element.getElementsByTagName(“ExpiryDate”)
.item(0).getTextContent().toString().trim();
System.out.println(“ExpiryDate Tag in Xml ==>”
+ xmlExpiry);
xmlActivationDate = element
.getElementsByTagName(“ActivationDate”).item(0)
.getTextContent().toString().trim();
System.out.println(“ActivationDate Tag in Xml ==>”
+ xmlActivationDate);
}

System.out.println(“Item Tag Completed”);

}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
System.out.println(“XML File Reading completed”);
}

}

[/java]

 

 

Console Output:

[plain]

XML File Reading Started
Item Tag Started
Code Tag in Xml ==>OMGFNP15A
Merchant Tag in Xml ==>Ferns N Petals Pvt. Ltd.
Title Tag in Xml ==>Flat 15% off on Sitewide
VoucherCodeId Tag in Xml ==>37668
TrackingUrl Tag in Xml ==>http://clk.omgt5.com/?PID=7970&AID=372827&CID=4674691&MID=176598&r=http%3a%2f%2fwww.fnp.com%2f
ExpiryDate Tag in Xml ==>31/10/2016 00:00:00
ActivationDate Tag in Xml ==>01/10/2016 00:00:00
Item Tag Completed
Item Tag Started
Code Tag in Xml ==>FNPOMG17
Merchant Tag in Xml ==>Ferns N Petals Pvt. Ltd.
Title Tag in Xml ==>Flat 17% off on Sitewide
VoucherCodeId Tag in Xml ==>37669
TrackingUrl Tag in Xml ==>http://clk.omgt5.com/?PID=7970&AID=372827&CID=4674691&MID=176598&r=http%3a%2f%2fwww.fnp.com%2f
ExpiryDate Tag in Xml ==>31/10/2016 00:00:00
ActivationDate Tag in Xml ==>01/10/2016 00:00:00
Item Tag Completed
Item Tag Started
Code Tag in Xml ==>FNPOMGD100
Merchant Tag in Xml ==>Ferns N Petals Pvt. Ltd.
Title Tag in Xml ==>Flat Rs. 100 off on minimum purchase of Rs. 799
VoucherCodeId Tag in Xml ==>37670
TrackingUrl Tag in Xml ==>http://clk.omgt5.com/?PID=7970&AID=372827&CID=4674691&MID=176598&r=http%3a%2f%2fwww.fnp.com%2f
ExpiryDate Tag in Xml ==>31/10/2016 00:00:00
ActivationDate Tag in Xml ==>01/10/2016 00:00:00
Item Tag Completed
XML File Reading completed

[/plain]

 

Download samplexml.xml file

 

 

Feel free to share any thoughts/comments in the below comments section.

Leave a Reply