- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 JAXB 新手,在解码时遇到问题。我使用以下 XSD 生成 JAXB 类:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://last.fm/ge0rgi"
xmlns="http://last.fm/ge0rgi"
elementFormDefault="qualified">
<xs:complexType name="lfm-response-type">
<xs:choice>
<xs:element name="error" type="error-type" minOccurs="1"
maxOccurs="1" />
<xs:element name="lovedtracks" type="lovedtracks-type"
minOccurs="1" maxOccurs="1" />
</xs:choice>
<xs:attribute type="status-type" name="status" use="required" />
</xs:complexType>
<xs:simpleType name="status-type">
<xs:restriction base="xs:string">
<xs:enumeration value="ok"/>
<xs:enumeration value="failed"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="error-type">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:integer" name="code" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="lovedtracks-type">
<xs:sequence>
<xs:element name="track" type="track-type" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="user" type="xs:string" use="required"/>
<xs:attribute name="page" type="xs:integer" use="required"/>
<xs:attribute name="perPage" type="xs:integer" use="required"/>
<xs:attribute name="totalPages" type="xs:integer" use="required"/>
<xs:attribute name="total" type="xs:integer" use="required"/>
</xs:complexType>
<xs:complexType name="date-type">
<xs:simpleContent>
<xs:extension base="xs:string">
<!-- Unix timestamp min and max not set -->
<xs:attribute name="uts" type="xs:integer" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="artist-type">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1"
maxOccurs="1" />
<xs:element name="mbid" type="xs:string" minOccurs="1"
maxOccurs="1" />
<xs:element name="url" type="xs:anyURI" minOccurs="1"
maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="size-type">
<xs:restriction base="xs:string">
<xs:enumeration value="small" />
<xs:enumeration value="medium" />
<xs:enumeration value="large" />
<xs:enumeration value="extralarge" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="image-type">
<xs:simpleContent>
<xs:extension base="xs:anyURI">
<xs:attribute name="size" type="size-type" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="streamable-type">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="fulltrack" type="xs:integer"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="track-type">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1"
maxOccurs="1" />
<xs:element name="mbid" type="xs:string" minOccurs="1"
maxOccurs="1" />
<xs:element name="url" type="xs:anyURI" minOccurs="1"
maxOccurs="1" />
<xs:element name="date" type="date-type" minOccurs="1"
maxOccurs="1" />
<xs:element name="artist" type="artist-type" minOccurs="1"
maxOccurs="1" />
<xs:element name="image" type="image-type" minOccurs="0"
maxOccurs="unbounded" />
<xs:element name="streamable" type="streamable-type" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:element name="lfm" type="lfm-response-type" />
然后我在 LfmResponseType 类的顶部添加了这一行: @XmlRootElement(name = "lfm", namespace ="\"http://last.fm/ge0rgi")
XML 的一部分如下所示:
<lfm status="ok"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://last.fm/ge0rgi'
xsi:schemaLocation='http://last.fm/ge0rgi lovedtracks.xsd'>
<lovedtracks user="RJ" page="1" perPage="50" totalPages="13"
total="633" >
<track>
<name>Badge</name>
<mbid>5b78d6ef-8a86-457c-bee3-4bc6fe234df9</mbid>
<url>http://www.last.fm/music/Cream/_/Badge</url>
<date uts="1392473900">15 Feb 2014, 14:18</date>
<artist>
<name>Cream</name>
<mbid>04cd0cfd-bfd1-4c36-bc38-95c35e2c045f</mbid>
<url>http://www.last.fm/music/Cream</url>
</artist>
<image size="small">http://userserve-ak.last.fm/serve/34s/60060455.png
</image>
<image size="medium">http://userserve-ak.last.fm/serve/64s/60060455.png
</image>
<image size="large">http://userserve-ak.last.fm/serve/126/60060455.png
</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/300x300/60060455.png
</image>
<streamable fulltrack="0">0</streamable>
</track>
...
解码代码是:
JAXBContext context = JAXBContext.newInstance(LfmResponseType.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
LfmResponseType response = (LfmResponseType) unmarshaller.unmarshal(new File("src/jaxbtest/lovedtracks.xml"));
异常
UnmarshalException: unexpected element (uri:"http://last.fm/ge0rgi", local:"lfm"). Expected elements are <{"http://last.fm/ge0rgi}lfm>
据我了解,这是一个命名空间问题,但我必须知道如何修复它。
编辑:生成的类LfmResponseType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "lfm-response-type", propOrder = {
"error",
"lovedtracks"
})
@XmlRootElement(name = "lfm", namespace ="\"http://last.fm/ge0rgi\"")
public class LfmResponseType {
protected ErrorType error;
protected LovedtracksType lovedtracks;
@XmlAttribute(name = "status", required = true)
protected StatusType status;
public ErrorType getError() {
return error;
}
public void setError(ErrorType value) {
this.error = value;
}
public LovedtracksType getLovedtracks() {
return lovedtracks;
}
public void setLovedtracks(LovedtracksType value) {
this.lovedtracks = value;
}
public StatusType getStatus() {
return status;
}
public void setStatus(StatusType value) {
this.status = value;
}
}
最佳答案
您只需使用 JAXBContext 注册整个生成的包即可:
JAXBContext context = JAXBContext.newInstance("fm.last.ge0rgi");
此外,您不需要添加 @XMlRootElement 标记,您可以这样做:
JAXBElement<LfmResponseType> response = (JAXBElement<LfmResponseType>) unmarshaller.unmarshal(is);
LfmResponseType responseType = response.getValue());
关于java - 解码时出现 JAXB 意外元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21954281/
我是一名优秀的程序员,十分优秀!