Untitled
unknown
plain_text
2 years ago
2.2 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 sitzungsnrr = xpath.compile("//sitzungsnr");
XPathExpression vornamee = xpath.compile("//vorname");
XPathExpression nachnamee = xpath.compile("//nachname");
XPathExpression fraktionn = xpath.compile("//fraktion");
// Evaluate the XPath expression against the document
NodeList nodeList = (NodeList) sitzungsnr.evaluate(document, XPathConstants.NODESET);
NodeList nodeList1 = (NodeList) vornamee.evaluate(document, XPathConstants.NODESET);
NodeList nodeList2 = (NodeList) nachnamee.evaluate(document, XPathConstants.NODESET);
NodeList nodeList3 = (NodeList) fraktionn.evaluate(document, XPathConstants.NODESET);
System.out.println(nodeList.item(i).getTextContent()+ ". Sitzung");
// Output all 'vorname' values
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println("Vorname: " + nodeList.item(i).getTextContent());
System.out.println("Nachname: " + nodeList1.item(i).getTextContent());
System.out.println("Fraktion: " + nodeList2.item(i).getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}Editor is loading...
Leave a Comment