How to parse XML & read tag values using JDOM parser?
This blog explains how to read the below xml and also parses the child and the tag value of the given child. I am using Jdom dependency here which you can download from here.
Table of Contents
Sample XML File:
For more xml samples you can refer w3schools link.
<?xml version="1.0" encoding="UTF-8" ?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>Light Belgian waffles covered with strawberries and whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description>Thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> <calories>950</calories> </food> </breakfast_menu>
Parsing the XML using Jdom Parser:
Here I have parsed all the food names presence inside the food tag.
package com.ngdeveloper.parser; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import java.io.IOException; import java.io.StringReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; public class XmlPropertyRead { public static void main(String[] args) throws Exception { parseXmlUsingJdom(); } public static void parseXmlUsingJdom() throws Exception { Path relPath = Paths.get("src", "test", "resources", "my_sample_xml.xml"); String content = null; try { content = Files.lines(relPath).collect(Collectors.joining(System.lineSeparator())); String xml = content; SAXBuilder saxBuilder = new SAXBuilder(); Document doc = saxBuilder.build(new StringReader(xml)); Element rootNode = doc.getRootElement(); List<Element> dataItemsList = rootNode.getChildren("food"); for (int i = 0; i < dataItemsList.size(); i++) { Element dataItem = (Element) dataItemsList.get(i); System.out.println(dataItem.getChild("name").getValue()); } } catch (IOException e) { e.printStackTrace(); } } }
Output:
Belgian Waffles Strawberry Belgian Waffles Berry-Berry Belgian Waffles French Toast Homestyle Breakfast
How to get the value based on the xpath given ?
First learn how to create the xpath for the xml elements or attributes here.
Here I am going to get the first food tag name using xpath.
//breakfast_menu/food[1]/name
Code to get the value of the above given xpath:
public static void getXpathValue(Document document){ String xPath = "//breakfast_menu/food[1]/name"; XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> expr = xFactory.compile(xPath, Filters.element()); Element attribute = expr.evaluateFirst(document); if (attribute != null) { System.out.println(attribute.getValue()); } else { System.out.println("foobar"); } }