当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Soap response header element parsing through JAXB

问题描述:

I have the following soap response header from which I want to unmarshall the value of the element using JAXB.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://example.com" xmlns:ass="http://example.com">

<soapenv:Header>

<com:HeaderResponse>

<com:RequestId>1234</com:RequestId>

</com:HeaderResponse>

</soapenv:Header>

<soapenv:Body>

......................

</soapenv:Body>

</soapenv:Envelope>

I have tried many options but not able to get the value of RequestID element. One of the method is:

package com.example.model;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "HeaderResponse", propOrder = {

"RequestId"

})

@XmlRootElement(name = "HeaderResponse")

public class Header {

@XmlElement(name = "RequestId",required = true)

protected String RequestId;

public String getRequestId() {

return RequestId;

}

public void setRequestId(String RequestId) {

this.RequestId = RequestId;

}

}

Calling code

JAXBContext jaxbContext = JAXBContext.newInstance(Header.class);

SOAPMessage requestMessage = MessageFactory.newInstance().createMessage(null,

new ByteArrayInputStream(file.getBytes()));;

SOAPHeader reqheader = requestMessage.getSOAPHeader();

Iterator<SOAPHeaderElement> itr = reqheader.examineAllHeaderElements();

while (itr.hasNext()) {

SOAPHeaderElement ele = itr.next();

Header headerType=(Header)jaxbContext.createUnmarshaller().unmarshal(ele);

System.out.println(headerType.getRequestId());

}

The exception which I'm getting:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com", local:"HeaderResponse"). Expected elements are <{}HeaderResponse>

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)

at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)

at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)

at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)

at com.example.model.XYZ.parseAndPrintXMLTagValues(XYZ.java:69)

at com.example.main.RunJaxB.main(RunJaxB.java:18)

网友答案:

Do not write your own code. just get the XSD file and generate java classes using the XSD file and then you will able to get proper data: For refrence: https://dzone.com/articles/using-jaxb-generate-java

分享给朋友:
您可能感兴趣的文章:
随机阅读: