How To Parse Or Read Xml File Inward Coffee >> Xml Tutorial Example

How to parse xml file in Java or how to read xml file inward java is 1 of mutual require of a Java Developer working alongside company Java application which uses XML for information representation, messaging as well as information transfer. Java has expert back upward to handgrip XML files as well as XML Documents as well as you lot tin read XML File inward Java, create or write to XML file inward Java past times using diverse XML parsers available. Reading XML file is petty fleck unlike than reading text or binary file inward Java but it uses same concept of File class.


 is 1 of mutual require of a Java Developer working alongside company Java application which  How to Parse or Read XML File inward Java  >> XML Tutorial ExampleUniversal acceptability of XML as well as Java has helped them to grow together as well as they cause got lot of things mutual inward betwixt simply similar Java is platform independence, XML provides information which is also platform independent. You tin role XML to transfer information betwixt a legacy application written inward C or C++ as well as Java.
What is of import to function alongside XML inward Java is right agreement of XML Parser, Basic cognition of XML document etc. In this Java XML Tutorial nosotros volition meet how to parse as well as XML File past times using both DOM  XML Parser. We volition also meet difference betwixt DOM as well as SAX parser inward XML and other basics related to XML parsing inward Java. I idea near this article afterwards sharing my xpath notes inward Java.

How to read XML File inward Java

JAXP - Java API for XML Parsing

Java provides extensive back upward for reading XML file, writing XML file as well as accessing whatever chemical constituent 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 inward javax.xml.parsers package.we volition meet illustration of parsing xml files using JAXP API inward side past times side section.


Parse XML File inward Java using DOM Parser

In this department nosotros volition meet how to parse xml files or how to read xml file using DOM XML Parser.DOM is quick as well as tardily agency to parse xml files inward Java as well as if you lot are doing it for testing its agency to go. Only affair to delineate of piece of employment concern is that XML files which require to hold upward parsed must non hold upward also large. You tin also create xml file past times using DOM parser as well as DocumentFactory Class inward Java.

XML file for parsing inward Java
Here is xml file Stocks.xml which contains to a greater extent than or less stocks as well as at that topographic point price, quantity nosotros volition role this inward our xml parsing illustration inward 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 inward Java using DOM Parser

Here is a code illustration of parsing inward a higher house xml file inward 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 constituent = (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 inward java for now. We cause got seen how to read as well as write xml file inward Java as well as instantly familiar alongside both DOM as well as SAX Parser inward java. We volition meet to a greater extent than on xml on coming days similar reading xml elements via xpath as well as using xml beans etc. permit me know if you lot cause got whatever doubtfulness on xml parsing illustration or inward full general alongside xml as well as Java.

Further Reading
The Complete Java Masterclas - Updated for Java 11
How to practise File as well as Directory inward Java

Belum ada Komentar untuk "How To Parse Or Read Xml File Inward Coffee >> Xml Tutorial Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel