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 ルート タグの null コンテンツが返されるだけです。
なにが問題ですか?どうすれば修正できますか?どんな助けでも感謝します。
アップデート。
@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">
これらは、root になるべき ONIXMessage タグの最初の後継者ですが、ONIXMessage タグ自体については言及されていないようです。