Search code examples
javaxmlbindingapache-axisunmarshalling

javax.xml.bind.UnmarshalException: unexpected element (uri:"MyProtocol.xsd", local:"MyFrame"). Expected elements are (none)


I am trying to unmarshal a XML file but keep getting this error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"MyProtocol.xsd", local:"MyFrame"). Expected elements are (none)

I am using Axis2 to generate the classes with ADB binding from my wsdl file. This is the root of the .wsdl:

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:xs="http://www.w3.org/2001/XMLSchema/"
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="MyProtocol.xsd"
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd"
    xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="MyProtocol.xsd">

    <types>
        <xs:schema targetNamespace="MyProtocol.xsd"
            xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="MyProtocol.xsd"
            elementFormDefault="qualified">

            <xs:import namespace="http://ws-i.org/profiles/basic/1.1/xsd"
                schemaLocation="http://ws-i.org/profiles/basic/1.1/swaref.xsd" />

            <xs:element name="MyFrame">
                <xs:complexType>
                    <xs:choice maxOccurs="unbounded">
                        ...

This is the code I have to unmarshal, which I'm assuming doesn't work with Axis2 and ADB bindings.

public class Foobar<T>
{
   private T obj;
   private Class<T> type;

   public Foobar(Class<T> type) {
       this.type = type;
   }

   public void unmarshalXML(String xml) {
       JAXBContext jaxbContext;
       Unmarshaller unmarshaller;
       StringReader reader;

       try {
           jaxbContext = JAXBContext.newInstance(type);
           unmarshaller = jaxbContext.createUnmarshaller();

           reader = new StringReader(xml);
           obj = (T) unmarshaller.unmarshal(reader);

       } catch(JAXBException e) {
           e.printStackTrace();
       }
   }
}

Axis2 generates a stub class which has all the getters, setters, and classes from the choices in my schema. I use that class and call the unmarshal method like this:

Foobar<MyStub.MyFrame> foobar = new Foobar<MyStub.MyFrame>(MyStub.MyFrame.class);

// Unmarshal
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
           + "<MyFrame xmlns=\"MyProtocol.xsd\">"
           + "<YC>"
           + "<SenderId>172</SenderId>"
           + "<RequestId>123saA</RequestId>"
           + "<SubNumber>5558879876</SubNumber>"
           + "</YC>"
           + "</MyFrame>";

foobar.unmarshalXML(xml);

I then get the error posted above. Why is this happening? Is my code wrong?


Solution

  • Never mind, I figured it out. If you have this XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xml>
    <MyFrame>
        <Data>
            <ID>172</ID>
            <Name>101</Name>
            <Date>11241987</Date>
        </Data>
    </MyFrame>
    

    You can unmarshal it with the below code. The code in the OP only works if using JAXB. However, in my scenario, I was using ADB.

    Unmarshal

    XMLStreamReader reader = XMLInputFactory.newInstance()
                .createXMLStreamReader(new ByteArrayInputStream(someXMLString.getBytes()));
    
    SomeClass myClass = SomeClass.Factory.parse(reader);
    

    Marshal (If you want to get the above XML from a class):

    OMElement omElement = myClass.getOMElement
                    (SomeClass.MY_QNAME, OMAbstractFactory.getSOAP12Factory());
    String someXMLString = omElement.toStringWithConsume();