unexpected element (uri:"", local:""). Expected elements are (none)

2787184 picture 2787184 · Aug 3, 2017 · Viewed 12.8k times · Source

I am not able to map response xml to java generated by xsd using cxf-xjc-plugin.

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-xjc-plugin</artifactId>
                <version>2.3.0</version>
                <configuration>
                    <extensions>
                        <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:2.3.0</extension>
                    </extensions>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-sources-trans</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xsdtojava</goal>
                        </goals>
                        <configuration>
                            <sourceRoot>${basedir}/src/main/java</sourceRoot>
                            <xsdOptions>
                                <xsdOption>
                                    <xsd>src/main/resources/Response.xsd</xsd>
                                    <packagename>com.test.response</packagename>
                                    <extensionArgs>
                                        <extensionArg>-Xdv</extensionArg>
                                    </extensionArgs>
                                </xsdOption>                                
                                </xsdOption>
                            </xsdOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Response.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="urn:automatedIDgenerationResponse" xmlns:tns="urn:automatedIDgenerationResponse" xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!-- definition of simple elements -->
    <complexType name="generatedIDs">
            <sequence>
                <element name="idType" type="string"/>
                <element name="id" type="string"/>
            </sequence>
    </complexType>
    <element name="automatedIDgenerationResponse" type="tns:automatedIDResponse"/>
    <complexType name="automatedIDResponse">
        <sequence>
            <element name="requestID" type="string" nillable="false"/>
            <element name="status" type="string" nillable="false"/>
            <element name="errorCode" type="int" nillable="false"/>
            <element name="errorText" type="string" nillable="true"/>
            <element name="timestamp" type="string" nillable="true"/>
            <element name="generatedIDs" type="tns:generatedIDs"  maxOccurs="unbounded"  />
        </sequence>
    </complexType>
</schema>

AutomatedIDResponse.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "automatedIDResponse", propOrder = {
    "requestID",
    "status",
    "errorCode",
    "errorText",
    "timestamp",
    "generatedIDs"
})
public class AutomatedIDResponse {

    @XmlElement(required = true)
    protected String requestID;
    @XmlElement(required = true)
    protected String status;
    protected int errorCode;
    @XmlElement(required = true, nillable = true)
    protected String errorText;
    @XmlElement(required = true, nillable = true)
    protected String timestamp;
    @XmlElement(required = true)
    protected List<GeneratedIDs> generatedIDs;

    //getter setter
}

GeneratedIDs.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "generatedIDs", propOrder = {
    "idType",
    "id"
})
public class GeneratedIDs {

    @XmlElement(required = true)
    protected String idType;
    @XmlElement(required = true)
    protected String id;
    //getter-setter
}   

main.java

public static void main(String[] args) {
        try {
        String xml=<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                    <ns3:automatedIDgenerationResponse xmlns:ns2="urn:automatedIDgeneration" xmlns:ns3="urn:automatedIDgenerationResponse">
                        <requestID>432012</requestID>
                        <status>Failure</status>
                        <errorCode>7000</errorCode>
                        <errorText>002098 The subscriber name already exists in the database. [code 2]</errorText>
                        <timestamp>Thu Aug 03 12:37:24 BST 2017</timestamp>
                    </ns3:automatedIDgenerationResponse>;


        StringReader reader = new StringReader(xml);
        JAXBContext jaxbContext2 = JAXBContext.newInstance(AutomatedIDResponse.class);
        Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();
        AutomatedIDResponse obj = (AutomatedIDResponse) jaxbUnmarshaller2.unmarshal(reader);
        }catch (JAXBException e) {
            System.out.println("JAXBException" + e);
        }
    }

error :

JAXBExceptionjavax.xml.bind.UnmarshalException: unexpected element (uri:"urn:automatedIDgenerationResponse", local:"automatedIDgenerationResponse"). Expected elements are (none)

Answer

2787184 picture 2787184 · Aug 3, 2017

I am able to convert xml to Object this way:

public static AutomatedIDResponse xmlToObject(String xmlString) throws XMLStreamException, UnsupportedEncodingException, JAXBException{
        InputStream stream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
        JAXBContext jaxbContext2 = JAXBContext.newInstance(AutomatedIDResponse.class);
        Unmarshaller jaxbUnmarshaller2 = jaxbContext2.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<AutomatedIDResponse> userElement = jaxbUnmarshaller2.unmarshal(someSource, AutomatedIDResponse.class);
        AutomatedIDResponse responseObject = userElement.getValue();
        return responseObject;
    }