[英]Jsoup : SelectorParseException when colon in xml tag
Exception is thrown when xml tag has colon, 当xml标记有冒号时抛出异常,
Exception: 例外:
org.jsoup.select.Selector$SelectorParseException: Could not parse query 'w:r': unexpected token at ':r' org.jsoup.select.Selector $ SelectorParseException:无法解析查询'w:r':':r'处的意外标记
XML: XML:
<w:r>
<w:rPr>
<w:rStyle w:val="jid"/>
</w:rPr>
<w:t>AN</w:t>
</w:r>
Java code: Java代码:
org.jsoup.nodes.Document doc = Jsoup.parse(documentXmlString);
Here documentXmlString has the xml specified above 这里documentXmlString具有上面指定的xml
Just replace ":" with "|" 只需将“:”替换为“|”
doc.select("w|r");
I'm using Jsoup 1.5.2. 我正在使用Jsoup 1.5.2。
Though your patchwork has worked for you.. I would like to give knowledge on namespace ! 虽然你的拼凑工作对你有用..我想提供关于命名空间的知识!
the w:
in your XML is actually called namespace prefix. w:
在您的XML中实际上称为名称空间前缀。 And to use neamespace prefix it has to be declared in the root node! 要使用neamespace前缀,必须在根节点中声明! 1+ Since the declaration part was missing in your source XML!
1+由于源XML中缺少声明部分! parser was throwing an error!
解析器抛出错误! Below is the way to define namespace in XML!
下面是在XML中定义命名空间的方法! I have corrected your own XML, I bet it wouldn't error-out now!
我已经更正了你自己的XML,我敢打赌它现在不会错误!
<w:r xmlns:w="http://www.w3.org/SomeNamespace">
<w:rPr>
<w:rStyle w:val="jid"/>
</w:rPr>
<w:t>AN</w:t>
</w:r>
Additional information: 附加信息:
The namespace has its own scope! 命名空间有自己的范围! in the below example:
在下面的例子中:
<root>
<w:r xmlns:w="http://www.w3.org/SomeNamespace">
<w:rPr>
<w:rStyle w:val="jid"/>
</w:rPr>
<w:t>AN</w:t>
</w:r>
<someotherElement>
<dummychild/>
</someotherElement>
In the above example, you cannot use namespace prefix on <someotherElement>
or <dummychild/>
!! 在上面的示例中,您不能在
<someotherElement>
或<dummychild/>
上使用名称空间前缀!! because the scope of namespace prefix w is upto element <r>
and its child (grandchild) only! 因为命名空间前缀w的范围只是元素
<r>
及其子(孙)的唯一!
1+:The Element under which Namespace is declared.. the namespace will be valid for itself and its child nodes.. Declaring namespace under root makes namespace valid/available for all the elements in XML Document. 1+:声明Namespace的元素。命名空间对其自身及其子节点有效。在root下声明命名空间使命名空间对XML Document中的所有元素有效/可用。
我用了,
documentXmlString = documentXmlString.replaceAll("w:","w");
JSoup is a html not an XML parser. JSoup是一个HTML而不是XML解析器。 For XML you can use JAXB or SAXON or Xstream.
对于XML,您可以使用JAXB或SAXON或Xstream。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.