On Wednesday, August 01, 2012 10:48:54 AM Bengt Rodehav wrote:
> After some testing (by editing the code generated by cxf-codegen-plugin)
> it turns out that its not the missing @XmlElement that causes my problem
> but the missing @XmlRootElement.
>
> Since the XML I'm trying to unmarshal is not a root element, no
> @XmlRootElement is generated (which I assume is as it should be). If I
> manually add the following:
>
> *@XmlRootElement(name = "logEntries", namespace = "
> http://www.digia.se/wsdl/cws/v2")*
>
> then the unmarshalling works.
>
> How can I unmarshal a subtree (fragment) using JaxB? Is
> *@*XmlRootElement(name required?
You may be able to use the unmarshal call that takes the class as the extra
parameter:
unMarshaller.unmarshal(new StreamSource(new StringReader(theXml)),
LogEntries.class);
I'm not 100% sure that works, but that would at least allow the context to
konw that it's supposed to unmarshal into that specific class.
Dan
>
> /Bengt
>
> 2012/8/1 Bengt Rodehav <[email protected]>
>
> > I have been using cxf-codegen-plugin to generate java code from wsdl. We
> > publish a web service using Cxf 2.4.2. It has worked fine but I now have
> > a requirement to unmarshal an XML string into java objects. The string
> > represents part of the result that my web service shall return. I do
> > this as follows (same code works in other applications):
> >
> > * final JAXBContext context =
> > JAXBContext.newInstance(LogEntries.class);*
> > * final Unmarshaller unMarshaller = context.createUnmarshaller();*
> > * LogEntries logEntries = (LogEntries) unMarshaller.unmarshal(new
> > StreamSource(new StringReader(theXml)));*
> >
> > Thus, "LogEntries" is a java class generated using cxf-codegen-plugin.
> > The above fails with the following exception:
> >
> > *Exception in thread "main" javax.xml.bind.UnmarshalException:
> > unexpected
> > element (uri:"http://www.digia.se/wsdl/cws/v2", local:"logEntries").
> > Expected elements are (none)*
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEven
> > t(UnmarshallingContext.java:642) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:
> > 254) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:
> > 249) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildEle
> > ment(Loader.java:116) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRoo
> > tLoader.childElement(UnmarshallingContext.java:1049) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElem
> > ent(UnmarshallingContext.java:478) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startEleme
> > nt(UnmarshallingContext.java:459) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXCo
> > nnector.java:148) *
> > * at
> > com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElemen
> > t(Unknown Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanSta
> > rtElement(Unknown Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSConte
> > ntDriver.scanRootElementHook(Unknown Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$F
> > ragmentContentDriver.next(Unknown Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDri
> > ver.next(Unknown Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unkn
> > own Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Un
> > known Source)*
> > * at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s
> > canDocument(Unknown Source)*
> > * at
> > com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unkn
> > own Source)*
> > * at
> > com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unkn
> > own Source)*
> > * at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown
> > Source)*
> > * at
> > com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unkno
> > wn Source)*
> > * at
> > com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.pars
> > e(Unknown Source)*
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unm
> > arshallerImpl.java:211) *
> > * at
> > com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unma
> > rshallerImpl.java:184) *
> > * at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown
> > Source)*
> > * at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown
> > Source)*
> > * at
> > se.capitalc.cws.ws.server.integration.cxa.impl.CXAServiceImpl.string2Log
> > Entries(CXAServiceImpl.java:789) *
> > * at
> > se.capitalc.cws.ws.server.integration.cxa.impl.CXAServiceImpl.main(CXASe
> > rviceImpl.java:1020) *
> >
> > What surprsed me is the message: "Expected elements are (none)". I
> > therefore looked at the generated code. Here is an excerpt from the
> > LogEntries class:
> >
> > *@XmlAccessorType(XmlAccessType.FIELD)*
> > *@XmlType(name = "LogEntries", propOrder = {*
> > * "logEntry"*
> > *})*
> > *public class LogEntries {*
> > *
> > *
> > * protected List<LogEntry> logEntry;*
> > *...*
> >
> > Note that the "logEntry" field is not annotated with @XmlElement. I
> > think
> > this is the reason of the exception but I do not understand why there is
> > no XmlElement annotation.
> >
> > I have a wsdl that imports several xml schema files. One of my web
> > service methods return a "History" complex type:
> >
> > * <element name="getHistoryResponse" type="tns:History" />*
> >
> > The History type is defined in a separate file, History.xsd as follows:
> >
> > *<?xml version="1.0" encoding="UTF-8"?>*
> > *<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="
> > http://www.digia.se/wsdl/cws/v2"*
> > * xmlns:tns="http://www.digia.se/wsdl/cws/v2"
> > elementFormDefault="qualified" attributeFormDefault="unqualified">*
> > *
> > *
> > * <complexType name="LogEntries">*
> > * <sequence>*
> > * <element name="logEntry" minOccurs="0" maxOccurs="unbounded"
> > type="tns:LogEntry"/>*
> > * </sequence>*
> > * </complexType>*
> > *
> > *
> > * <complexType name="LogEntry">*
> > * <sequence>*
> > * <element name="changedValues" type="tns:ChangedValues"
> > minOccurs="0" maxOccurs="1"/>*
> > * <element name="allValues" type="tns:AllValues" minOccurs="0"
> > maxOccurs="1"/>*
> > * </sequence>*
> > * <attribute name="by" type="string" use="required"/>*
> > * <attribute name="when" type="dateTime" use="required"/>*
> > * <attribute name="logType" type="tns:LogType" use="required"/>*
> > * <attribute name="entityType" type="tns:EntityType"
> > use="required"/>*
> > * </complexType>*
> > *
> > *
> > * <complexType name="ChangedValues">*
> > * <sequence>*
> > * <element name="changedValue" minOccurs="0" maxOccurs="unbounded"
> > type="tns:ChangedValue"/>*
> > * </sequence>*
> > * </complexType>*
> > *
> > *
> > * <complexType name="AllValues">*
> > * <sequence>*
> > * <element name="value" minOccurs="0" maxOccurs="unbounded"
> > type="tns:Value"/>*
> > * </sequence>*
> > * </complexType>*
> > *
> > *
> > * <complexType name="ChangedValue">*
> > * <sequence>*
> > * <element name="name" type="string"/>*
> > * <element name="before" type="string" minOccurs="0"/>*
> > * <element name="after" type="string" minOccurs="0"/>*
> > * </sequence>*
> > * </complexType>*
> > *
> > *
> > * <complexType name="Value">*
> > * <sequence>*
> > * <element name="name" type="string"/>*
> > * <element name="value" type="string"/>*
> > * </sequence>*
> > * </complexType>*
> > *
> > *
> > * <simpleType name="LogType">*
> > * <annotation>*
> > * <documentation>*
> > * N = New*
> > * U = Update*
> > * D = Delete*
> > * </documentation>*
> > * </annotation>*
> > * <restriction base="string">*
> > * <enumeration value="N"/>*
> > * <enumeration value="U"/>*
> > * <enumeration value="D"/>*
> > * </restriction>*
> > * </simpleType>*
> > *
> > *
> > * <simpleType name="EntityType">*
> > * <annotation>*
> > * <documentation>*
> > * C = Customer*
> > * </documentation>*
> > * </annotation>*
> > * <restriction base="string">*
> > * <enumeration value="C"/>*
> > * </restriction>*
> > * </simpleType>*
> > *
> > *
> > *
> > *
> > * <complexType name="History">*
> > * <sequence>*
> > * <element name="logEntries" type="tns:LogEntries" />*
> > * </sequence>*
> > * </complexType>*
> > *</schema>*
> >
> > I invoke the cxf-codegen-plugin as follws:
> >
> > * <plugin>*
> > * <groupId>org.apache.cxf</groupId>*
> > * <artifactId>cxf-codegen-plugin</artifactId>*
> > * <version>${cxf.version}</version>*
> > * <executions>*
> > * <execution>*
> > * <id>Wsdl2java</id>*
> > * <phase>generate-sources</phase>*
> > * <configuration>*
> > * <sourceRoot>${project.build.directory}/generated-sources*
> > * </sourceRoot>*
> > * <wsdlOptions>*
> > * <wsdlOption>*
> > *
> > <wsdl>${basedir}/src/main/resources/wsdl/cwsfull.wsdl</wsdl>*
> > * <extraargs>*
> > * <extraarg>-p</extraarg>*
> > * <extraarg>se.capitalc.cws.api</extraarg>*
> > * <extraarg>-wsdlLocation</extraarg>*
> > * <wsdlurl />*
> > * </extraargs>*
> > * </wsdlOption>*
> > * </wsdlOptions>*
> > * </configuration>*
> > * <goals>*
> > * <goal>wsdl2java</goal>*
> > * </goals>*
> > * </execution>*
> > * </executions>*
> > * </plugin>*
> >
> > Can anyone help me to understand why the XmlElement annotations are
> > missing and possibly advice me how to fix this?
> >
> > /Bengt
--
Daniel Kulp
[email protected] - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com