我有一个 XML 文件,如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<cpe-list>
<generator>
<product_name>National Vulnerability Database (NVD)</product_name>
<product_version>2.25</product_version>
<schema_version>2.3</schema_version>
<timestamp>2014-06-12T03:50:01.201Z</timestamp>
</generator>
<cpe-item name="cpe:/a:1024cms:1024_cms:0.7">
<title xml:lang="en-US">1024cms.org 1024 CMS 0.7</title>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.2.5">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.2.5</title>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.3.1">
<title xml:lang="en-US">1024cms.org 1024 CMS 1.3.1</title>
</cpe-item>
<cpe-item name="cpe:/a:1024cms:1024_cms:1.4.1">
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.4.1:*:*:*:*:*:*:*"/>
<title xml:lang="en-US">1024cms.org 1024 CMS 1.4.1</title>
<references>
<reference href="https://code.google.com/p/sexy-polling/downloads/list">version information</reference>
<reference href="http://2glux.com/projects/sexypolling">product information</reference>
</references>
</cpe-item>
</cpe-list>
我正在使用 JAXB 解析器来解析此 XMl 文件。
如何指定此标签的注释。
<cpe-23:cpe23-item name="cpe:2.3:a:1024cms:1024_cms:1.4.1:*:*:*:*:*:*:*"/>
因为这个给出了 SAXException。
@XmlElement(name="cpe-23",namespace="cpe23-item", required=false)
[org.xml.sax.SAXParseException; systemId: file:/home/Downloads/NIST/random.xml; lineNumber: 19; columnNumber: 79; The prefix "cpe-23" for element "cpe-23:cpe23-item" is not bound.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:523)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:220)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:189)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
最佳答案
为什么出现错误
在与 XML 模式相对应的 XML 中,:
之前的名称部分称为前缀。前缀实际上并不是名称的一部分,而是指在元素中声明的命名空间,例如
xmlns:cpe-23="http://www.example.com/cpe23"
在您的例子中,元素名称只是 cpe-23:cp23-item
并且 JAXB 正在使用的命名空间感知解析器正在被抛弃。
你可以做什么
您可以使用不支持命名空间的 SAX 解析器(默认)来解析文档,并指定 JAXB UnmarshallerHandler
作为 ContentHandler
来执行对象转换。
在您的属性上,您可以指定 @XmlElement
注释,例如:
@XmlElement(name="cpe-23:cp23-item")
关于java - 标签的 JAXB 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263042/