XML Parsing for Java
Let us take an example,
If there is a requirement in project that we need to parse xml file from a given location and you have to read the data from some specific tags.
There is a sample xml file.
<?xml version = "1.0"?>
<root>
<primary-data>
<fpn>201302</fpn>
<image>abc.html</image>
<process>MO</process>
</primary-data>
</root>
Here, we need to take simple steps in writing code as below:-
package com.xmlclass;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.*;
public class TagRead {
public static void main(String[] args)
{
//https://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm#ADXDK3000
try
{
File inputFile = new File("D:\\Java DOCX\\DOM XML Parser\\tagdata.xml");
//Create a DocumentBuilder
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
//Extract the root element
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("primary-data");
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("fpn tag value: "+eElement.getElementsByTagName("fpn").item(0).getTextContent());
System.out.println("image tag value: "+eElement.getElementsByTagName("image").item(0).getTextContent());
System.out.println("process tag value: "+eElement.getElementsByTagName("process").item(0).getTextContent());
}
}
}
catch (ParserConfigurationException e)
{
System.out.println("Check Parser configuration");
e.printStackTrace();
} catch (SAXException e)
{
System.out.println("xml parsing exception");
e.printStackTrace();
} catch (IOException e)
{
System.out.println("File Not found for parsing");
e.printStackTrace();
}
}
}
Let us take another example, where you need the xml as below:-
<?xml version = "1.0"?>
<root>
<primary-data>
<fpn>201302</fpn>
<image>abc.html</image>
<process>MO</process>
</primary-data>
<process_data>
<firstName>Heera</firstName>
<lastName>Babu</lastName>
</process_data>
</root>
Here, we need to take simple steps in writing code as below:-
package com.xmlclass;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.*;
public class TagRead {
public static void main(String[] args)
{
//https://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.htm#ADXDK3000
try
{
File inputFile = new File("D:\\Java DOCX\\DOM XML Parser\\tagdata.xml");
//Create a DocumentBuilder
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
//Extract the root element
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("primary-data");
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("fpn tag value: "+eElement.getElementsByTagName("fpn").item(0).getTextContent());
System.out.println("image tag value: "+eElement.getElementsByTagName("image").item(0).getTextContent());
System.out.println("process tag value: "+eElement.getElementsByTagName("process").item(0).getTextContent());
}
}
NodeList pList = doc.getElementsByTagName("process_data");
for (int temp = 0; temp < pList.getLength(); temp++)
{
Node nNode = pList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
System.out.println("firstName tag value: "+eElement.getElementsByTagName("firstName").item(0).getTextContent());
System.out.println("lastName tag value: "+eElement.getElementsByTagName("lastName").item(0).getTextContent());
}
}
}
catch (ParserConfigurationException e)
{
System.out.println("Check Parser configuration");
e.printStackTrace();
} catch (SAXException e)
{
System.out.println("xml parsing exception");
e.printStackTrace();
} catch (IOException e)
{
System.out.println("File Not found for parsing");
e.printStackTrace();
}
}
}
Sample Output:
Root element :root
Current Element :primary-data
fpn tag value: 201302
image tag value: abc.html
process tag value: MO
Current Element :process_data
firstName tag value: Heera
lastName tag value: Babu