我正在尝试使用JAXB从XML生成对象。首先,我根据提供的模式创建了对象,并将它们打包到1 .jar中。现在我正在使用这个简单的测试程序(在它拒绝与泽西岛一起工作之后):
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-string (没有空格或换行):
<?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标记,它就在那里。当我使用泽西时,它甚至没有显示任何内容-只返回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">这些都是应该是根的ONIXMessage标记的第一个继承者,但是似乎没有提到ONIXMessage标记本身。
发布于 2013-09-18 10:35:33
您正在解组的XML文档似乎不符合用于生成模型的XML架构。您可以尝试以下方法之一:
发布于 2013-09-18 09:57:32
我最好的猜测是,当生成context1时,我会将要解析的类(ONIXMessage)作为参数传递给(ONIXMessage)。我会试着用这样的代码:
public <T> T toObject(final String requestXML, final Class<T> type)
{
try
{
byte[] bytes = requestXML.getBytes("UTF-8"); //$NON-NLS-1$
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputSource inputSource = new InputSource(bais);
SAXSource source = new SAXSource(inputSource);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, type);
return (T) JAXBIntrospector.getValue(jaxbElement);
}
catch (Exception e)
{
ex.printStackTrace();
}
}**注意我把<T>放在哪里,应该去上ONIXMessage课。
1
发布于 2013-12-02 18:37:22
关于将Onix模式生成到Java类中需要注意的两件事。
@javax.xml.bind.annotation.XmlSchema(namespace = "",elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
编辑:看起来,您可以切换到一个请求绑定模式,该模式可以生成更好的类文件,但需要在构建过程中向架构添加配置元素(尽管否则不会更改)。https://jaxb.java.net/2.1.2/docs/vendorCustomizations.html#simple
https://stackoverflow.com/questions/18868815
复制相似问题