我试图解组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的工作方式 - 在我的例子中,它创建了一个{{1}而是改为。
答案 0 :(得分:0)
您的xsd缺少targetNamespace属性。你可以在Xml schema: empty targetNamespace
看到一个很好的解释答案 1 :(得分:0)
尝试这个:
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:”“,本地:”元素“)。 预期的元素是(无)“
请参阅此文章:http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html
希望这有帮助!