XML File
Create new package named data. In this package, create new xml file named products.xml as below:
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>p01</id>
<name>name 1</name>
<price currency="usd">20</price>
<quantity>5</quantity>
<weight unit="kg">2.1</weight>
<date format="dd/MM/yyyy">02/03/2017</date>
<status>true</status>
</product>
<product>
<id>p02</id>
<name>name 2</name>
<price currency="eur">12</price>
<quantity>3</quantity>
<weight unit="kg">6.5</weight>
<date format="dd/MM/yyyy">24/11/2018</date>
<status>true</status>
</product>
<product>
<id>p03</id>
<name>name 3</name>
<price currency="aud">13</price>
<quantity>1</quantity>
<weight unit="kg">7.8</weight>
<date format="dd/MM/yyyy">14/10/2018</date>
<status>false</status>
</product>
</products>
Run Application
Create new package, named demo. In this package, create new java class, named Main as below
package demo;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;
public class Main {
private static void display() {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
DOMImplementation domImplementation = documentBuilder.getDOMImplementation();
if (!domImplementation.hasFeature("traversal", "2.0")) {
System.out.println("A DOM implementation that supports traversal is required.");
return;
}
Document document = documentBuilder.parse("src\\data\\products.xml");
DocumentTraversal documentTraversal = (DocumentTraversal) document;
TreeWalker treeWalker = documentTraversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
traverseNodes(treeWalker);
} catch (Exception ex) {
}
}
private static void traverseNodes(TreeWalker treeWalker) {
Node node = treeWalker.getCurrentNode();
if (node.getNodeName().equalsIgnoreCase("id")) {
System.out.println("Id : " + node.getTextContent());
}
if (node.getNodeName().equalsIgnoreCase("name")) {
System.out.println("Name : " + node.getTextContent());
}
if (node.getNodeName().equalsIgnoreCase("price")) {
System.out.println("Price : " + node.getTextContent());
}
if (node.getNodeName().equalsIgnoreCase("quantity")) {
System.out.println("Quantity : " + node.getTextContent());
}
if (node.getNodeName().equalsIgnoreCase("weight")) {
System.out.println("Weight : " + node.getTextContent());
}
if (node.getNodeName().equalsIgnoreCase("date")) {
System.out.println("Date : " + node.getTextContent());
}
if (node.getNodeName().equalsIgnoreCase("status")) {
System.out.println("Status : " + node.getTextContent());
}
if (node.hasAttributes()) {
System.out.println("\tAttributes: " + node.getAttributes().getLength());
NamedNodeMap namedNodeMap = node.getAttributes();
for (int i = 0; i < namedNodeMap.getLength(); i++) {
System.out.println("\t" + namedNodeMap.item(i).getNodeName() + ":" + namedNodeMap.item(i).getTextContent());
}
}
for (Node n = treeWalker.firstChild(); n != null; n = treeWalker.nextSibling()) {
traverseNodes(treeWalker);
}
treeWalker.setCurrentNode(node);
}
public static void main(String[] args) {
display();
}
}
Output
Id : p01
Name : name 1
Price : 20
Attributes: 1
currency:usd
Quantity : 5
Weight : 2.1
Attributes: 1
unit:kg
Date : 02/03/2017
Attributes: 1
format:dd/MM/yyyy
Status : true
Id : p02
Name : name 2
Price : 12
Attributes: 1
currency:eur
Quantity : 3
Weight : 6.5
Attributes: 1
unit:kg
Date : 24/11/2018
Attributes: 1
format:dd/MM/yyyy
Status : true
Id : p03
Name : name 3
Price : 13
Attributes: 1
currency:aud
Quantity : 1
Weight : 7.8
Attributes: 1
unit:kg
Date : 14/10/2018
Attributes: 1
format:dd/MM/yyyy
Status : false