javax.xml.bind.UnmarshalException:意外元素(uri:"MyProtocol.xsd",本地:"MyFrame")。预期元素是 (none)

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

我正在尝试解组一个 XML 文件,但不断收到此错误:

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

我正在使用 Axis2 从我的 wsdl 文件生成带有 ADB 绑定的 classes。这是 .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">
                        ...

这是我必须解组的代码,我假设它不适用于 Axis2 和 ADB 绑定。

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 生成一个存根 class,其中包含我模式中选择的所有 getter、setter 和 classes。我使用 class 并像这样调用 unmarshal 方法:

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);

然后我收到上面发布的错误。为什么会这样?我的代码有错吗?

没关系,我想通了。如果你有这个 XML:

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

您可以使用以下代码对其进行解组。 OP 中的代码仅在使用 JAXB 时有效。但是,在我的场景中,我使用的是 ADB。

解组

XMLStreamReader reader = XMLInputFactory.newInstance()
            .createXMLStreamReader(new ByteArrayInputStream(someXMLString.getBytes()));

SomeClass myClass = SomeClass.Factory.parse(reader);

Marshal(如果你想从class得到上面的XML):

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