gpt4 book ai didi

java - JAXB Umarshaller 错误

转载 作者:行者123 更新时间:2023-11-29 03:29:46 27 4
gpt4 key购买 nike

我正在尝试使用 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>jbk@globalbookinfo.com</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 时,它似乎要求一个正确的元素。但根据架构,第一个元素(根)是 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 这样的元素定义 - 但如果没有 Root 元素的定义,我无法理解 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com