java - JAXB Umarshaller 错误

标签 java xml jaxb xsd jersey

我正在尝试使用 JAXB 从 XML 生成对象。首先,我根据提供的架构创建了对象并将它们打包到 1 个 .jar 中。现在我正在使用这个简单的测试程序(在它拒绝与 Jersey 一起工作之后):

import org.editeur.ns.onix._3_0.reference.*;

public class Mapping {

    public static void main(String[] args) {
        System.out.println("Please enter the Onix message:");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String request = reader.readLine();
            request.trim();
            StringReader stringReader = new StringReader(request);
            JAXBContext context = JAXBContext.newInstance("org.editeur.ns.onix._3_0.reference");
            Unmarshaller unmarshaller = context.createUnmarshaller();           
            ONIXMessage message = (ONIXMessage) unmarshaller.unmarshal(stringReader);   
            stringReader.close();
            //System.out.println(message.getHeader().getSentDateTime());
            //System.out.println(message.getHeader().getMessageNote().get(0).getValue());
        } catch(Exception ex) {
            ex.printStackTrace();
        }

    }
}

我使用以下测试 XML 字符串(不带空格或换行符):

   <?xml version="1.0" encoding="UTF-8"?><ONIXMessage release="3.0"><Header><Sender><SenderName>Global Bookinfo</SenderName><ContactName>Jane King, +1 555 321 7654</ContactName><EmailAddress><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="78121a13381f14171a19141a17171311161e17561b1715" rel="noreferrer noopener nofollow">[email protected]</a></EmailAddress></Sender><Addressee><AddresseeName>BooksBooksBooks.com</AddresseeName></Addressee><MessageNumber>231</MessageNumber><SentDateTime>20100510T1115-0400</SentDateTime><MessageNote>Sample message</MessageNote></Header><Product><RecordReference>com.globalbookinfo.onix.01734529</RecordReference><NotificationType>03</NotificationType><RecordSourceType>04</RecordSourceType><RecordSourceIdentifier><RecordSourceIDType>06</RecordSourceIDType><IDValue>0614141800001</IDValue></RecordSourceIdentifier><RecordSourceName>Global Bookinfo</RecordSourceName><ProductIdentifier><ProductIDType>03</ProductIDType><IDValue>9780007232833</IDValue></ProductIdentifier><ProductIdentifier><ProductIDType>15</ProductIDType><IDValue>9780007232833</IDValue></ProductIdentifier></Product></ONIXMessage>

But it gives me the same error:

    javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ONIXMessage"). Expected elements are <{http://ns.editeur.org/onix/3.0/reference}Addressee>,<{http://ns.editeur.org/onix/3.0/reference}AddresseeIDType>,<{http://ns.editeur.org/onix/3.0/reference}AddresseeIdentifier>,<{http://ns.editeur.org/onix/3.0/reference}AddresseeName>,<{http://ns.editeur.org/onix/3.0/reference}Affiliation>,<{http://ns.editeur.org/onix/3.0/reference}AgentIDType>,<{http://ns.editeur.org/onix/3.0/reference}AgentIdentifier>,<{http://ns.editeur.org/onix/3.0/reference}AgentName>,<{http://ns.editeur.org/onix/3.0/reference}AgentRole>,<{http://ns.editeur.org/onix/3.0/reference}AlternativeName>,<{http://ns.editeur.org/onix/3.0/reference}AncillaryContent>,<{http://ns.editeur.org/onix/3.0/reference}AncillaryContentDescription>,<{http://ns.editeur.org/onix/3.0/reference}AncillaryContentType>,<{http://ns.editeur.org/onix/3.0/reference}Audience>,<{http://ns.editeur.org/onix/3.0/reference}AudienceCode> 

...


        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:648)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:236)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:231)
        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:1051)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:484)
        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:501)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
        at Mapping.main(Mapping.java:19)

当我用XJC生成类时,没有检测到任何问题。 但在解析 XML 时,它似乎要求提供正确的元素。但根据 Schema,第一个元素(根)是 ONIXMessage 标签,它就在那里。 当我使用 Jersey 时,它甚至不显示任何内容 - 仅返回 ONIXMessage 根标记的空内容。

出了什么问题?我该如何修复它?任何帮助表示赞赏。

更新。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header",
    "product"
})
@XmlRootElement(name = "ONIXMessage")
public class ONIXMessage {

    @XmlElement(name = "Header", required = true)
    protected Header header;
    @XmlElement(name = "Product", required = true)
    protected List<Product> product;
    @XmlAttribute
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String refname;
    @XmlAttribute
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String shortname;
    @XmlAttribute
    protected String datestamp;
    @XmlAttribute
    protected String sourcetype;
    @XmlAttribute
    @XmlSchemaType(name = "anySimpleType")
    protected String sourcename;
    @XmlAttribute(required = true)
    protected String release;

这是XJC生成的根类。看起来应该是这样。我不太了解模式,但在模式中似乎没有像 ONIXMessage 这样的元素定义 - 但如果没有根元素的定义,我无法理解 XJC 从哪里获取它。这是 XSD 的开始:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://ns.editeur.org/onix/3.0/reference" xmlns="http://ns.editeur.org/onix/3.0/reference">
    <xs:include schemaLocation="ONIX_BookProduct_CodeLists.xsd"/>
    <xs:include schemaLocation="ONIX_XHTML_Subset.xsd"/>
    <xs:element name="Addressee">
        <xs:complexType>
            <xs:sequence>
                <xs:choice>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" ref="AddresseeIdentifier"/>
                        <xs:element minOccurs="0" ref="AddresseeName"/>
                    </xs:sequence>
                    <xs:element ref="AddresseeName"/>
                </xs:choice>
                <xs:element minOccurs="0" ref="ContactName"/>
                <xs:element minOccurs="0" ref="EmailAddress"/>
            </xs:sequence>
            <xs:attribute name="refname">
                <xs:simpleType>
                    <xs:restriction base="xs:token">
                        <xs:enumeration value="Addressee"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="shortname">
                <xs:simpleType>
                    <xs:restriction base="xs:token">
                        <xs:enumeration value="addressee"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attributeGroup ref="generalAttributes"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="AddresseeIdentifier">

这些是“should-be-root ONIXMessage”标记的第一个后继者,但似乎没有提及 ONIXMessage 标记本身。

最佳答案

您正在解码的 XML 文档似乎不符合您用于生成模型的 XML 架构。您可以尝试以下方法之一:

  • 根据 XML 架构验证输入 XML,看看哪里无效。
  • 填充 Java 对象并将它们编码为 XML,以查看输入 XML 的外观。

关于java - JAXB Umarshaller 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18868815/

相关文章:

java - JAXB 解码器的handleEvent 未被调用

java - 在 PUT Restful 服务中使用 JSON 对象

java - 如何使用 Java exec 通过 tee 传输 telnet

android - android中的dom xml解析

c# - 使用 AvalonEdit 显示无效的 XML 语法

c# - (不?)使用 JavaScriptSerializer 将 xml 文件(未知模式)转换为 c# 中的 json

java - 如何通知xjc不要走xs :import?

java - 有没有办法在 GridBagLayout 中设置固定的行和列?

java - 在 View 子类中实现监听器是否存在问题?

java - Java Swing 中的键盘 "Held"事件?