简体   繁体   中英

JAXB “Unexpected element” error

I'm trying to unmarshal an XML document. I generate the classes using XJC.

The XSD:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Element">
    <xsd:complexType>
      <xsd:sequence>
      ....
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

The XML:

<Element>
....
</Element>

The generated classes:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    ....
})
public class Element {
   .....
}

@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public Element createElement() {
        return new Element();
    }
}

I use Spring OXM as follows:

@Configuration
public class MySpringConfig {

    @Bean
    public Unmarshaller unmarshaller() {
        Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
        // org.example.jaxb2 is the package where the Element and ObjectFactory classes are located
        unmarshaller.setPackagesToScan("org.example.jaxb2");
        return unmarshaller;
    }
}

I then use the Unmarshaller as follows:

Object obj = unmarshaller.unmarshal(source);

And I get the following error:

org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (URI : "", local : "Element"). Expected elements are (none)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:884)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:758)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:735)
    at 
......
Caused by: javax.xml.bind.UnmarshalException: unexpected element (URI : "", local : "Element"). Expected elements are (none)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:380)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:614)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3135)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:123)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:754)
    ... 33 more

I searched on the web but can't find anything helpful. I'm aware that the Element class is not annotated with @XmlRootElement , but according to the articles I found, it is the way XJC works - in my case, it creates an ObjectFactory class instead.

your xsd has the targetNamespace attribute missing. you can see a good explanation about this at Xml schema: empty targetNamespace

Try maybe this:

   public static Element unmarshal(File source) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Element .class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    JAXBElement<Element> root = jaxbUnmarshaller.unmarshal(new StreamSource(
            source), Element.class);
    Element el = root.getValue();

    LOGGER.info(el.toString());
    return el;
   }

For more info about the problems from type:

" unexpected element (uri:"", local:"Element"). Expected elements are (none) "

See this article : http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

Hope this helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM