java code to get all leaf nodes of a xml file
try {
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("input.xml");
final XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//*[count(./*) = 0]");
final NodeList nodeList = (NodeList) xpath.evaluate(doc, XPathConstants.NODESET);
for(int i = 0; i < nodeList.getLength(); i++) {
final Element el = (Element) nodeList.item(i);
System.out.println(el.getNodeName());
}
} catch (Exception e) {
e.printStackTrace();
}
Also in Java:
- how to count the number of occurrences of an element in a arraylist in java
- java convert String to int
- run spring boot application command line
- how to count an replace string in java
- python to java converter
- countdown timer with seekbar
- java quit application
- find the day of the week Calendar class in java
- java change boolean to opposite
- java random number
- java string regexp replace
- Java sort Map by values
- java stream sort
- java command line arguments
- creating thread in java example
- java get command line output
- convert string to int java
- java get icon for file type
- java how to print a string[]
- indexof java
- Caused by: java.lang.NumberFormatException: Invalid int: ""
- get file path java
- java how to read a text file
- java create file