Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将SOAP XML解组到Java对象_Java_Xml_Soap_Jaxb - Fatal编程技术网

如何将SOAP XML解组到Java对象

如何将SOAP XML解组到Java对象,java,xml,soap,jaxb,Java,Xml,Soap,Jaxb,在尝试将soap XML解组到JAXB对象时,我遇到以下错误 我们得到一个错误,预期的元素为“无”。在解组SOAP XML时是否应该执行任何特定的操作 javax.xml.bind.JAXBContext jaxbContext = (javax.xml.bind.JAXBContext) JAXBContext.newInstance(Class.forName(requestName)); Unmarshaller unmarshaller = jaxbContext.createUnmar

在尝试将soap XML解组到JAXB对象时,我遇到以下错误

我们得到一个错误,预期的元素为“无”。在解组SOAP XML时是否应该执行任何特定的操作

javax.xml.bind.JAXBContext jaxbContext = (javax.xml.bind.JAXBContext) JAXBContext.newInstance(Class.forName(requestName));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(SoapXmlString);          
reqInfo = unmarshaller.unmarshal(reader);
我收到以下错误:

 javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are (none)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(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.reportUnexpectedChildElement(Loader.java:116)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement
下面是示例XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://example.com/v2">
       <soapenv:Header/>
       <soapenv:Body>
          <v2:createSession>
             <v2:client>
                <!--Optional:-->
                <v2:name>?</v2:name>
                <!--Optional:-->
                <v2:clientId>?</v2:clientId>
                <!--Optional:-->
                <v2:requestId>?</v2:requestId>
             </v2:client>
             <!--Optional:-->
             <v2:oldSessionId>?</v2:oldSessionId>
             <!--Optional:-->
             <v2:clientIp>?</v2:clientIp>
             <!--Optional:-->
             <v2:clientIpStatus>?</v2:clientIpStatus>
             <!--Optional:-->
             <v2:superBYOBFlow>?</v2:superBYOBFlow>
             <!--Optional:-->
             <v2:FlowParams>?</v2:FlowParams>
             <!--Optional:-->
             <v2:deviceInfo>?</v2:deviceInfo>
          </v2:createSession>
       </soapenv:Body>
    </soapenv:Envelope>

?
?
?
?
?
?
?
?
?

请帮忙

我认为你没有考虑肥皂信封。。。您生成的JAXB解组器不会知道任何关于主体或信封标记的信息,它将期望您的createSession是根元素,因此出现“意外元素”错误

您需要首先从信封中提取内容,如果您首先从内容中创建SOAPMessage对象,则可以使用message.getSOAPBody().extractContentAsDocument()执行此操作

这是一个非常复杂的例子,下面是我的

字符串示例=
"";
SOAPMessage message=MessageFactory.newInstance().createMessage(null,
新的ByteArrayInputStream(例如.getBytes());
Unmarshaller Unmarshaller=JAXBContext.newInstance(Farm.class).createUnmarshaller();
Farm Farm=(Farm)unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
似乎如果您没有在schema.xsd文件中声明名称空间,那么您将看到您的错误

我使用根元素createSession创建了一个虚拟模式,通过添加targetNamespace属性并重新生成JAXB类,错误消失了

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     targetNamespace="http://example.com/v2"> <!-- targetNamespace essential for JAXB to work-->
    <xs:element name="createSession">  
        <xs:complexType>
            <xs:attribute name="foo" type="xs:string" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>


requestName有什么值?请参阅。@priyesh:它是一个类名。例如:CreateSessionRequest我可能会失明(很可能),但我看不到重复答案的链接?!?谢谢你的帮助。。我尝试了相同的代码,用CreateSessionRequest类替换Farm类。但我又犯了以下错误。javax.xml.bind.UnmarshaleException:意外元素(uri:,本地:“createSession”)。所需元素为(无)@srinath您能否发布您的.xsd文件,我会仔细查看。
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     targetNamespace="http://example.com/v2"> <!-- targetNamespace essential for JAXB to work-->
    <xs:element name="createSession">  
        <xs:complexType>
            <xs:attribute name="foo" type="xs:string" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>