Wednesday, 18 September 2013

JAXB Umarshaller error

JAXB Umarshaller error

I am trying to generate Object from XML using JAXB. First, I created the
objects out of supplied schema and packaged them into 1 .jar. Now I am
using this simple test program (after it refused to work with 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();
}
}
}
I use the following test XML-string (withouts spaces or line breaks):
<?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)
When I was generating classes with XJC, no problems were detected. But
when parsing XML, it seems to ask for a correct element. But according to
the Schema, the first element (root) is ONIXMessage tag, which is there.
When I am using Jersey, it does not even show anything - just returns null
contents of ONIXMessage root tag.
What is wrong? How can I fix it? Any help appreciated.

No comments:

Post a Comment