java 使用 XPATH 解析 XMl 时出现 org.xml.sax.SAXParseException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15563140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.xml.sax.SAXParseException while parsing XMl using XPATH
提问by John Christy
I am trying to get values from an XML using XPATH. I received the following exception:
我正在尝试使用 XPATH 从 XML 获取值。我收到以下异常:
[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)
I am getting this error because my XML is a little bit of different from normal one (please see below):
我收到此错误是因为我的 XML 与普通 XML 有点不同(请参见下文):
<?xml version="1.0" encoding="UTF-8"?>
<inventory>
<Sample>
<abc:priority>1</abc:priority>
<abc:value>2</abc:value>
</Sample>
</inventory>
Here is my code (Java) to get values from the above XML:
这是我从上述 XML 获取值的代码(Java):
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());
}
}
}
If I remove the semicolon, I never get this error.
如果我删除分号,则不会出现此错误。
Is it possible to get content from an XML like mentioned above using XPATH?
是否可以使用 XPATH 从上面提到的 XML 中获取内容?
回答by Daniel Haley
"Is it possible to get content from an XML like mentioned above using Xpath ?" - I don't think so. This XML isn't well-formed.
“是否可以使用 Xpath 从上面提到的 XML 中获取内容?” - 我不这么认为。此 XML 格式不正确。
From the spec (http://www.w3.org/TR/REC-xml-names/#ns-qualnames):
从规范(http://www.w3.org/TR/REC-xml-names/#ns-qualnames):
The Prefix provides the namespace prefix part of the qualified name, and MUST be associated with a namespace URI referencein a namespace declaration. [Definition: The LocalPart provides the local part of the qualified name.]
Prefix 提供限定名称的命名空间前缀部分,并且必须与命名空间声明中的命名空间 URI 引用相关联。[定义:LocalPart 提供限定名称的本地部分。]
In order to do anything with it, I think you'll have to add a namespace declaration.
为了用它做任何事情,我认为你必须添加一个命名空间声明。
Example
例子
<inventory xmlns:abc="x">
<Sample>
<abc:priority>1</abc:priority>
<abc:value>2</abc:value>
</Sample>
</inventory>
回答by forty-two
Try without this line:
尝试不使用此行:
domFactory.setNamespaceAware(true); // never forget this!
Although it normally is a bad idea to run without namespace awareness, in this specific case it makes sense, since the input file is the way it is.
尽管在没有命名空间意识的情况下运行通常是一个坏主意,但在这种特定情况下它是有道理的,因为输入文件就是这样。