Untitled
unknown
plain_text
2 years ago
1.5 kB
9
Indexable
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.NodeList;
public class XmlParser {
public static void main(String[] args) {
String xmlFilePath = "path/to/your/file.xml";
try {
// XML Parsing using DocumentBuilderFactory
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document document = docBuilderFactory.newDocumentBuilder().parse(xmlFilePath);
// Create an XPath expression to select all 'vorname' elements
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expression = xpath.compile("//vorname");
// Evaluate the XPath expression against the document
NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
// Output all 'vorname' values
System.out.println("All 'vorname' values:");
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Editor is loading...
Leave a Comment