JAXB "Unexpected element"错误



我正在尝试解组一个XML文档。我使用XJC生成类。

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>

XML:

<Element>
....
</Element>

生成的类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    ....
})
public class Element {
   .....
}
@XmlRegistry
public class ObjectFactory {
    public ObjectFactory() {
    }
    public Element createElement() {
        return new Element();
    }
}

我使用Spring OXM如下:

@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;
    }
}

然后我按如下方式使用Unmarshaller:

Object obj = unmarshaller.unmarshal(source);

我得到以下错误:

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

我在网上搜索了一下,但找不到任何有用的东西。我知道Element类没有用@XmlRootElement进行注释,但根据我发现的文章,这就是XJC的工作方式——在我的例子中,它创建了一个ObjectFactory类。

xsd缺少targetNamespace属性。您可以在Xml架构中看到关于这一点的很好的解释:empty targetNamespace

试试这个:

   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;
   }

有关问题的更多信息,请键入:

"意外元素(uri:",local:"element")。预期元素为(无)"

请参阅本文:http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

希望这能有所帮助!

相关内容

  • 没有找到相关文章

最新更新