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 java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
private static void display() {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("src\\data\\products.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpression = xpath.compile("//product");
NodeList products = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
System.out.println("Count Product: " + products.getLength());
for(int i = 0; i < products.getLength(); i++) {
Node node = products.item(i);
String id = xpath.compile("./id").evaluate(node);
System.out.println("Id: " + id);
String name = xpath.compile("./name").evaluate(node);
System.out.println("Name: " + name);
double price = Double.parseDouble(xpath.compile("./price").evaluate(node));
System.out.println("Price: " + price);
String currencyAttr = xpath.compile("./price/@currency").evaluate(node);
System.out.println("\tCurrency: " + currencyAttr);
int quantity = Integer.parseInt(xpath.compile("./quantity").evaluate(node));
System.out.println("Quantity: " + quantity);
double weight = Double.parseDouble(xpath.compile("./weight").evaluate(node));
System.out.println("Weight: " + weight);
String unitAttr = xpath.compile("./weight/@unit").evaluate(node);
System.out.println("\tUnit: " + unitAttr);
Date date = simpleDateFormat.parse(xpath.compile("./date").evaluate(node));
System.out.println("Date: " + simpleDateFormat2.format(date));
String formatAttr = xpath.compile("./date/@format").evaluate(node);
System.out.println("\tFormat: " + formatAttr);
boolean status = Boolean.parseBoolean(xpath.compile("./status").evaluate(node));
System.out.println("Status: " + status);
System.out.println("=============================");
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static void main(String[] args) {
display();
}
}
Output
Count Product: 3
Id: p01
Name: name 1
Price: 20.0
Currency: usd
Quantity: 5
Weight: 2.1
Unit: kg
Date: 2017-03-02
Format: dd/MM/yyyy
Status: true
=============================
Id: p02
Name: name 2
Price: 12.0
Currency: eur
Quantity: 3
Weight: 6.5
Unit: kg
Date: 2018-11-24
Format: dd/MM/yyyy
Status: true
=============================
Id: p03
Name: name 3
Price: 13.0
Currency: aud
Quantity: 1
Weight: 7.8
Unit: kg
Date: 2018-10-14
Format: dd/MM/yyyy
Status: false
=============================