1

我正在尝试学习 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)
4

2 回答 2

1

在您的用例中,您已经定义了根元素信息,利用@XmlElementDecl注释的ObjectFactory类上的注释@XmlRegistry(参见: http ://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html )。由于带有注释的类@XmlRegistry可以被称为任何东西,并且 JAXB 不进行包扫描,因此您需要将您ObjectFactory作为传入的类之一包含在内以引导JAXBContext.

JAXBContext.newInstance(AddRequest.class, ObjectFactory.class);

由于ObjectFactory类引用AddRequest,您可以将其简化为:

JAXBContext.newInstance(ObjectFactory.class);
于 2013-10-09T09:43:20.497 回答
1

尝试这个:

 try {
        FileInputStream inputStream = new FileInputStream(new File("your file"));
        AddRequest req = JAXB.unmarshal(inputStream, AddRequest.class);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
于 2013-10-09T10:05:53.247 回答