如何解决解组异常?

时间:2013-10-09 03:10:06

标签: jaxb xsd

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


}

ObjectFactory.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);
    }

}

package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/AddRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.example.addrequest;

Main.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();
        }

AddRequest.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)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

试试这个:

 try {
        FileInputStream inputStream = new FileInputStream(new File("your file"));
        AddRequest req = JAXB.unmarshal(inputStream, AddRequest.class);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }