javax.xml.bind.UnmarshalException:意外元素,预期元素为(无)

jum4pzuy  于 2023-03-11  发布在  Java
关注(0)|答案(6)|浏览(192)

我在解组字符串时遇到这个错误。我使用JAXB创建了Java文件。

JAXBContext jaxbContext = JAXBContext.newInstance(DocumentType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// Input string posted below
DocumentType dType = (DocumentType) unmarshaller.unmarshal(new StringReader("input string"));

堆栈跟踪:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd", local:"document"). Expected elements are (none)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3065)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
    at JaxbTest.main(JaxbTest.java:19)

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd"
    elementFormDefault="qualified">

    <xs:element name="document" type="tns:documentType"></xs:element>

    <xs:complexType name="documentType">
        <xs:sequence>
            <xs:element name="businessCard" type="tns:businessCardType" minOccurs="0" maxOccurs="1"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="businessCardType">
        <xs:sequence>
            <xs:element name="field" type="tns:fieldType" minOccurs="0" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="fieldType">
        <xs:sequence>
            <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1">
            </xs:element>
            <xs:element name="characters" type="tns:charactersType" minOccurs="0" maxOccurs="1"></xs:element>
            <xs:element name="fieldComponents" type="tns:fieldComponentsType" minOccurs="0" maxOccurs="1"></xs:element>
        </xs:sequence>
        <xs:attribute name="type" type="tns:typeType"></xs:attribute>
    </xs:complexType>

    <xs:simpleType name="typeType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Name"></xs:enumeration>
            <xs:enumeration value="Phone"></xs:enumeration>
            <xs:enumeration value="Mobile"></xs:enumeration>
            <xs:enumeration value="Fax"></xs:enumeration>
            <xs:enumeration value="Company"></xs:enumeration>
            <xs:enumeration value="Job"></xs:enumeration>
            <xs:enumeration value="Address"></xs:enumeration>
            <xs:enumeration value="Email"></xs:enumeration>
            <xs:enumeration value="Web"></xs:enumeration>
            <xs:enumeration value="Text"></xs:enumeration>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="charactersType">
        <xs:sequence>
            <xs:element name="char" type="tns:charType" minOccurs="0" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="charType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="suspicious" type="xs:boolean" use="optional"></xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="fieldComponentsType">
      <xs:sequence>
        <xs:element name="fieldComponent" type="tns:fieldComponentType" minOccurs="0" maxOccurs="unbounded"></xs:element>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="fieldComponentType">
      <xs:sequence>
        <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1">
        </xs:element>
      </xs:sequence>
      <xs:attribute name="type" type="tns:fieldComponentTypeType"></xs:attribute>
    </xs:complexType>

    <xs:simpleType name="fieldComponentTypeType">
      <xs:restriction base="xs:string">
        <xs:enumeration value="FirstName"></xs:enumeration>
        <xs:enumeration value="MiddleName"></xs:enumeration>
        <xs:enumeration value="LastName"></xs:enumeration>
        <xs:enumeration value="ExtraName"></xs:enumeration>
        <xs:enumeration value="PhonePrefix"></xs:enumeration>
        <xs:enumeration value="PhoneCountryCode"></xs:enumeration>
        <xs:enumeration value="PhoneCode"></xs:enumeration>
        <xs:enumeration value="PhoneBody"></xs:enumeration>
        <xs:enumeration value="PhoneExtension"></xs:enumeration>
        <xs:enumeration value="ZipCode"></xs:enumeration>
        <xs:enumeration value="Country"></xs:enumeration>
        <xs:enumeration value="City"></xs:enumeration>
        <xs:enumeration value="StreetAddress"></xs:enumeration>
        <xs:enumeration value="JobPosition"></xs:enumeration>
        <xs:enumeration value="JobDepartment"></xs:enumeration>
      </xs:restriction>
    </xs:simpleType>

</xs:schema>

输入字符串:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
<businessCard>
<field type="Phone">
  <value>783-37-00</value>
</field>
<field type="Phone">
  <value>783-37-01</value>
</field>
<field type="Fax">
  <value>783-26-63</value>
</field>
<field type="Email">
  <value>john_sm@abbyy.com</value>
</field>
<field type="Web">
  <value>www.abbyy.com</value>
</field>
<field type="Address">
  <value>Otradnaya str., 2b, bld.6, 127273, Moscow, Russia</value>
</field>
<field type="Name">
  <value>John Smith</value>
</field>
<field type="Company">
  <value>ABBYY Headquarters</value>
</field>
<field type="Job">
  <value>Product Analyst</value>
</field>
<field type="Text">
  <value>ABBYY ABBYY Headquarters John Smith Product Analyst ABBYY Headquarters Otradnaya str., 2b, bld.6, 127273, Moscow, Russia Tel: 783-37-00 Fax: 783-26-63 john_sm@abbyy.com www.abbyy.com </value>
</field>
</businessCard>
</document>
n9vozmp4

n9vozmp41#

当从XML模式生成JAXB模型时,对应于命名复杂类型的全局元素将元数据捕获为ObjectFactory类中create方法上的@XmlElementDecl注解。由于您只在DocumentType类上创建JAXBContext,因此不会处理此元数据。如果从XML模式生成JAXB模型,那么应该在生成的包名或ObjectFactory类上创建JAXBContext,以确保处理所有必要的元数据。

示例解决方案:

JAXBContext jaxbContext = JAXBContext.newInstance(my.generatedschema.dir.ObjectFactory.class);
DocumentType documentType = ((JAXBElement<DocumentType>) jaxbContext.createUnmarshaller().unmarshal(inputStream)).getValue();
ezykj2lf

ezykj2lf2#

或者,如果你想坚持使用DocumentType类,那么你可以在DocumentType类的顶部添加以下注解。

@XmlRootElement(name="document")

注意:String值"document"是xml消息根标签的名称。

wsewodh2

wsewodh23#

这个错误的原因之一是使用了来自JDK的jaxb实现。我不知道为什么这样的问题会出现在非常简单的xml解析情况下。您可以使用来自公共maven库的jaxb库的最新版本:
http://mvnrepository.com

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.12</version>
</dependency>
rkue9o1l

rkue9o1l4#

在我的例子中,当我错误地导入jakarta.xml.bind.annotation.XmlRootElement时,我得到了这个错误。

dphi5xsq

dphi5xsq5#

在我们的例子中,我们得到了UnmarshalException,因为在下面的代码中指定了一个错误的Java包。这个问题在正确的包到位后得到了解决:

@Bean
public Unmarshaller tmsUnmarshaller() {
    final Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller
            .setPackagesToScan("java.package.to.generated.java.classes.for.xsd");
    return jaxb2Marshaller;
}
iyfjxgzm

iyfjxgzm6#

在我的例子中,与Tuan Hoang的解决方案相反,我不得不使用jakarta.xml.bind而不是javax.xml.bind,这可能与项目中使用的JDK有关,在我的例子中是JDK11。

相关问题