How to parse xml file in Java or how to read xml file inwards java is i of mutual postulate of a Java Developer working amongst corporation Java application which uses XML for information representation, messaging as well as information transfer. Java has proficient back upwards to handgrip XML files as well as XML Documents as well as you lot tin strength out read XML File inwards Java, create or write to XML file inwards Java past times using diverse XML parsers available. Reading XML file is lilliputian fleck dissimilar than reading text or binary file inwards Java but it uses same concept of File class.
Universal acceptability of XML as well as Java has helped them to grow together as well as they get got lot of things mutual inwards betwixt merely similar Java is platform independence, XML provides information which is also platform independent. You tin strength out piece of occupation XML to transfer information betwixt a legacy application written inwards C or C++ as well as Java.
What is of import to piece of occupation amongst XML inwards Java is right agreement of XML Parser, Basic cognition of XML document etc. In this Java XML Tutorial nosotros volition run across how to parse as well as XML File past times using both DOM XML Parser. We volition also run across difference betwixt DOM as well as SAX parser inwards XML and other basics related to XML parsing inwards Java. I idea almost this article later sharing my xpath notes inwards Java.
How to read XML File inwards Java
JAXP - Java API for XML Parsing
Java provides extensive back upwards for reading XML file, writing XML file as well as accessing whatever chemical factor from
XML file. All XML parsing related classes as well as methods are within JAXP. Though DOM related code comes from org.w3c.dom package. All XML parsers are inwards javax.xml.parsers package.we volition run across representative of parsing xml files using JAXP API inwards side past times side section.
Parse XML File inwards Java using DOM Parser
In this department nosotros volition run across how to parse xml files or how to read xml file using DOM XML Parser.DOM is quick as well as slow means to parse xml files inwards Java as well as if you lot are doing it for testing its means to go. Only matter to trouble organisation is that XML files which postulate to survive parsed must non survive besides large. You tin strength out also create xml file past times using DOM parser as well as DocumentFactory Class inwards Java.
XML file for parsing inwards Java
Here is xml file Stocks.xml which contains around stocks as well as in that place price, quantity nosotros volition piece of occupation this inwards our xml parsing representative inwards Java.
<?xml version="1.0" encoding="UTF-8"?>
<stocks>
<stock>
<symbol>Citibank</symbol>
<price>100</price>
<quantity>1000</quantity>
</stock>
<stock>
<symbol>Axis bank</symbol>
<price>90</price>
<quantity>2000</quantity>
</stock>
</stocks>
Code Example of Parsing XML File inwards Java using DOM Parser
Here is a code representative of parsing higher upwards xml file inwards Java using DOM parser:
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 DOMExampleJava {
public static void main(String args[]) {
try {
File stocks = new File("Stocks.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document physician = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();
System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("stock");
System.out.println("==========================");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element chemical factor = (Element) node;
System.out.println("Stock Symbol: " + getValue("symbol", element));
System.out.println("Stock Price: " + getValue("price", element));
System.out.println("Stock Quantity: " + getValue("quantity", element));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}
Output:
root of xml file stocks
==========================
Stock Symbol: Citibank
Stock Price: 100
Stock Quantity: 1000
Stock Symbol: Axis bank
Stock Price: 90
Stock Quantity: 2000
That’s all on xml parsing inwards java for now. We get got seen how to read as well as write xml file inwards Java as well as directly familiar amongst both DOM as well as SAX Parser inwards java. We volition run across to a greater extent than on xml on coming days similar reading xml elements via xpath as well as using xml beans etc. allow me know if you lot get got whatever incertitude on xml parsing representative or inwards full general amongst xml as well as Java.
Further Reading
The Complete Java Masterclas - Updated for Java 11
How to practise File as well as Directory inwards Java