我有一个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)
答案 0 :(得分:1)
在对应于XML Schema的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")