Untitled
unknown
plain_text
2 years ago
2.6 kB
10
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;
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);
// XPath expressions for various elements in the XML
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
// Vorspann values
String sitzungOrt = evaluateXPath(xpath, document, "//vorspann/kopfdaten/sitzung-ort");
String sitzungDatum = evaluateXPath(xpath, document, "//vorspann/kopfdaten/sitzungstitel/veranstaltungsdaten/datum/@date");
// Inhaltsverzeichnis values (first entry)
String ivzEintragInhalt = evaluateXPath(xpath, document, "//inhaltsverzeichnis/ivz-eintrag[1]/ivz-eintrag-inhalt");
String rednerVorname = evaluateXPath(xpath, document, "//inhaltsverzeichnis/ivz-eintrag[1]/redner/vorname");
String rednerNachname = evaluateXPath(xpath, document, "//inhaltsverzeichnis/ivz-eintrag[1]/redner/nachname");
String rednerFraktion = evaluateXPath(xpath, document, "//inhaltsverzeichnis/ivz-eintrag[1]/redner/fraktion");
// Output values in a single line
System.out.println("Sitzung Ort: " + sitzungOrt);
System.out.println("Sitzung Datum: " + sitzungDatum);
System.out.println("Erster Eintrag im Inhaltsverzeichnis: " + ivzEintragInhalt);
System.out.println("Name des ersten Redners: " + rednerVorname + " " + rednerNachname + ", Fraktion: " + rednerFraktion);
// Fügen Sie hier weitere Ausgaben für andere Informationen ein
} catch (Exception e) {
e.printStackTrace();
}
}
private static String evaluateXPath(XPath xpath, Document document, String expression) throws Exception {
XPathExpression xpathExpression = xpath.compile(expression);
return (String) xpathExpression.evaluate(document, XPathConstants.STRING);
}
}
Editor is loading...
Leave a Comment