No URI javax.xml.bind.UnmarshalException: unexpect

2019-07-09 07:57发布

I am having trouble to unmarshall my data. I got the following error:

ERROR FsceClient - Error in getDataInMatches : unexpected element (uri:"", local:"SearchAndList"). Expected elements are (none) requested params:+COUNTRY=US+YR=2016+DIV=Ford+WB=122.0 javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"SearchAndList"). Expected elements are (none) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)

This is my xml file:

<SearchAndList>
    <fvd>
        +COUNTRY=US+YR=2016+DIV=Ford+WB=122.0
    </fvd>
    <sol>
    <rsi>
        <sType>Ss</sType>
        <mHave>true</mHave>
        <toAr>0</toAr>
        <toAr>0</toAr>
        <toAr>22</toAr>
    </rsi>
    <rsi>
        <sType>ssa</sType>
        <mHave>true</mHave>
        <toAr>77</toAr>
    </rsi>
    </sol>
    <sol>
        <rsi>
            <sType>sve</sType>
            <mHave>false</mHave>
            <toAr>0</toAr>
            <toAr>21</toAr>
        </rsi>
    </sol>
</SearchAndList>

标签: java xml rest jaxb
1条回答
2楼-- · 2019-07-09 08:39

This is encountered when the XSD schema does not contain element definitions and only contains class definitions (i.e. complex types).

e.g. for this XSD,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="foo">
    <xs:sequence>
      <xs:element name="bar" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

The object factory created is like this:

@XmlRegistry
public class ObjectFactory {
   public ObjectFactory() {
   }
   public Foo createFoo() {
        return new Foo();
    }
}

BUT FOR THIS XSD:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="foo" type="foo" nillable="true"/>
  <xs:complexType name="foo">
    <xs:sequence>
      <xs:element name="bar" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

The ObjectFactory class created by JAXB is:

@XmlRegistry
public class ObjectFactory {
    private final static QName _Foo_QNAME = new QName("", "foo");
    public ObjectFactory() {
    }
    public Foo createFoo() {
        return new Foo();
    }
    @XmlElementDecl(namespace = "", name = "foo")
    public JAXBElement<Foo> createFoo(Foo value) {
        return new JAXBElement<Foo>(_Foo_QNAME, Foo.class, null, value);
    }
}

You can see that the JAXBElement wrapper creation method is also added. With the second XSD, the unmarshaller knows what to do when it encounters a tag with name "foo". So if you have an XSD, add "element" definitions as well as the complex types.

----- EDIT---- The sample unmarshaller code:

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object result = ((JAXBElement<Object>) jaxbUnmarshaller.unmarshal(stream)).getValue();
查看更多
登录 后发表回答