我正在尝试学习 JAXB。我创建了如下示例,但在解组期间我遇到了异常。我的文件在下面。你能帮我解决吗?
AddRequest.java:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AddRequest", namespace = "http://www.example.org/AddRequest", propOrder = {
"first",
"sec",
"any"
})
public class AddRequest {
@XmlElement(name = "First")
protected int first;
@XmlElement(name = "Sec")
protected int sec;
@XmlAnyElement(lax = true)
protected List<Object> any;
}
对象工厂.java
@XmlRegistry
public class ObjectFactory {
private final static QName _AddRequest_QNAME = new QName("http://www.example.org/AddRequest", "AddRequest");
public ObjectFactory() {
}
public AddRequest createAddRequest() {
return new AddRequest();
}
@XmlElementDecl(namespace = "http://www.example.org/AddRequest", name = "AddRequest")
public JAXBElement<AddRequest> createAddRequest(AddRequest value) {
return new JAXBElement<AddRequest>(_AddRequest_QNAME, AddRequest.class, null, value);
}
}
包信息.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/AddRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.example.addrequest;
主.java
try {
File file = new File("C:\\Users\\nbkyooh\\IBM\\rationalsdp\\workspace\\Sample\\resource\\AddRequest.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(org.example.addrequest.AddRequest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
添加请求.xml
<?xml version="1.0" encoding="UTF-8"?>
<tns:AddRequest xmlns:tns="http://www.example.org/AddRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/AddRequest AddRequest.xsd ">
<tns:First>0</tns:First>
<tns:Sec>0</tns:Sec>
</tns:AddRequest>
我得到如下异常,我做错了什么。我使用了所有生成的文件。
javax.xml.bind.UnmarshalException: Unexpected element "{http://www.example.org/AddRequest}AddRequest". Expected elements are "".
at com.ibm.xml.xlxp2.jaxb.msg.JAXBMessageProvider.throwUnmarshalExceptionWrapper(JAXBMessageProvider.java:93)
at com.ibm.xml.xlxp2.jaxb.unmarshal.impl.DeserializationContext.handleSkippedRootElementEvent(DeserializationContext.java:318)
at com.ibm.xml.xlxp2.jaxb.unmarshal.impl.JAXBDocumentScanner.produceRootElementEvent(JAXBDocumentScanner.java:189)