So verwenden Sie XPath in Java, wenn Namespace -Präfixe instabil sind [Duplikat]Java

Java-Forum
Anonymous
 So verwenden Sie XPath in Java, wenn Namespace -Präfixe instabil sind [Duplikat]

Post by Anonymous »

Ich muss kleine Teile aus XML -Dateien in Java extrahieren. Ich möchte XPath dafür verwenden. Ich meine, manchmal bekomme ich " ..." som: title. namespace-uri()='http://www.my.hu/somethingo']/[local-name()='Title' and namespace-uri()='http://www.my.hu/somethingo']/*[local-name()='Object' and Namespace-uri () = 'http: //www.my.hu/otherhingo']
...
Aber die Pfade sind in diesem Formular ziemlich unlesbar. />

Code: Select all

public String extractObjectInTitleInDoc(String xml) throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "" +
"/*[local-name()='Doc' and namespace-uri()='http://www.my.hu/somethingo']" +
"/*[local-name()='Title' and namespace-uri()='http://www.my.hu/somethingo']" +
"/*[local-name()='Object' and namespace-uri()='http://www.my.hu/otherthingo']" +
"/text()" +
"";
String title = (String) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.STRING);

return title;
}
Ein minimales Arbeitsbeispiel des oben genannten ist hier:
https://github.com/riskop/java_xpath_lo ... espace_uri

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post