org.xml.sax.SAXParseException beim analysieren von XMl-Daten mithilfe von XPATH
Ich versuche Werte aus einem XML-Daten mithilfe von XPATH. Erhielt ich die folgende exception:
[Fatal Error] books.xml:4:16: The prefix "abc" for element "abc:priority" is not bound.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:///D:/XSL%20TEST%20APP%20BACK%20UP/XMLTestApp/books.xml; lineNumber: 4; columnNumber: 16; The prefix "abc" for element "abc:priority" is not bound.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at xpath.XPathExample.main(XPathExample.java:18)
Bin ich immer diese Fehlermeldung, weil mein XML ist ein wenig anders als das normale (siehe unten):
<?xml version="1.0" encoding="UTF-8"?>
<inventory>
<Sample>
<abc:priority>1</abc:priority>
<abc:value>2</abc:value>
</Sample>
</inventory>
Hier ist mein code (Java), um Werte aus dem obigen XML:
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class XPathExample {
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); //never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("books.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr
= xpath.compile("//Sample/*/text()");////book/Sample[author='Neal Stephenson']/title/text()
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}
Wenn ich entfernen Sie das Semikolon, bekomme ich nie diese Fehler.
Ist es möglich, Inhalte aus einer XML, wie oben erwähnt, die Verwendung von XPATH?
InformationsquelleAutor John Christy | 2013-03-22
Du musst angemeldet sein, um einen Kommentar abzugeben.
"Ist es möglich, Inhalte aus einer XML, wie oben erwähnt, die Verwendung von Xpath ?" - Ich glaube nicht. Dieses XML ist nicht wohlgeformt.
Aus der spec (http://www.w3.org/TR/REC-xml-names/#ns-qualnames):
In Ordnung, etwas zu tun, denke ich, werden Sie haben, um eine namespace-Deklaration.
Beispiel
Es ist, weil der Präfix
abc
ist nicht gebunden. Wenn//Sample/*/text()
funktioniert, können Sie versuchen, mit*[local-name()='priority']
statt*
nur.Bin immer null-Wert während der Verwendung *[local-name()='priority']. eigentlich möchte ich Lesen, ein webservice wird es wie folgt Aussehen
<?xml version="1.0" encoding="UTF-8"?> <abc:Event abc1:type="abc:Veranstaltung" xmlns:abc="schema.abc.com/abc" xmlns:abc1="w3.org/2001/XMLSchema-instance"> <abc:eventID>65c81c27-c79e-ce-9e4b-1d6c36f2f30c</abc:eventID> <abc:sequenceNumber>1</abc:sequenceNumber> <abc:userId>Beispiel</abc:userId> <abc:externalApplicationId>SampleApp</abc:externalApplicationId> <abc:subscriptionId>1e318cb7-84f2-40c6 </abc:subscriptionId> <abc:channelId>714353f-4313-4f5f-aa61-b912c7</abc:channelId> <abc:eventData-abc1:type="abc:NewSubscriptionEvent" /> </abc:Veranstaltung> Sorry, für die späte Antwort.
Kann ich die Benutzer-id mit Xpath? Wenn ja bitte um Rat. Thnks
InformationsquelleAutor Daniel Haley
Ausprobieren, ohne diese Zeile:
Obwohl es in der Regel eine schlechte Idee, um ohne namespace-Bewusstsein, in diesem speziellen Fall macht es Sinn, da die Eingabe-Datei ist, wie es ist.
InformationsquelleAutor forty-two