Skip to main content

SOAP (With XML body) UnMarshalling - creating Objects!!

4 replies [Last post]
javaistyle
Offline
Joined: 2012-09-19

Hi Guys,

I am new to java world & lookin to create objects -UnMarshal from SOAP having XML contents in it.

I tried using JAXB and tried Unmarshaller from google search but it was not so useful and getting error like below

Exception in thread "main" java.lang.IllegalStateException: reader must be on a START_ELEMENT event, not a 2 event

Can someone please guide me for it?

Cheers!!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
gmazza
Offline
Joined: 2005-01-14

Are you trying to create a SOAP web service--that marshalling/unmarshalling is normally done automatically for you: http://www.jroller.com/gmazza/entry/web_service_tutorial

Alternatively, you can place JAX-WS handlers on top of your SOAP service to parse the incoming XML manually if needed: http://www.jroller.com/gmazza/entry/jaxws_handler_tutorial

HTH,
Glen

javaistyle
Offline
Joined: 2012-09-19

Thanks Gmazza!!

I tried to get help from the links you provided but was not helpful for my purpose.

Actually my purpose is to modify SOAP request in one file say abc1.xml to abc2.xml though JAXB Unmarshalling and Marshalling.

Please check the Questions I have put as below - Any help would be appreciated... Thanks a ton

For Example, I am trying as below

public class SoapRequestAbc1ToAbc2 {
public static void main(String[] args) throws Exception{
MessageFactory msgFactory = MessageFactory.newInstance();
MimeHeaders mimeHeaders = new MimeHeaders();
mimeHeaders.addHeader("Content-Type","text/xml; charset=UTF-8");
FileInputStream file = new FileInputStream("C:\\abc1.xml");
SOAPMessage message = msgFactory.createMessage(mimeHeaders, file);
message.writeTo(System.out);
System.out.println("Trying to accessing the SOAPBody");
SOAPBody soapBody = message.getSOAPBody();
Iterator i = soapBody.getChildElements();
file.close();

SOAPPart soapPart = message.getSOAPPart(); // Question1 - How to get print of it? SOAPEnvelope envelope = soapPart.getEnvelope(); // Question2 - How to get Print of it? SOAPHeader header = envelope.getHeader(); // Question3 - How to get Print of it?
//Iterating through Soap Body and Printing each of it's child element and their values

soapBody = message.getSOAPBody();
if ( soapBody != null ) {
Iterator iterator = soapBody.getChildElements();
while (iterator.hasNext()) {
.....
..... // Question4- Is this Iteration Correct? How to make it complete with the SYSout for each element? }
}

// Question -5 : How to UNMARSHALL the soap elements using JAXB from the above?
// Question -6 : How to modify the objects as from Step 5 and writing back the SOAP to new file, say, abc2.xml

}
}

gmazza
Offline
Joined: 2005-01-14

That looks painful--can't you just use XSLT to change the SOAP messages? SOAP is XML after all. Here's an unrelated XSLT example if you'd like starter code: http://www.jroller.com/gmazza/entry/soap_contract_xslt_cleanup

HTH,
Glen

javaistyle
Offline
Joined: 2012-09-19

That's the approach to change one xml to another.
But In that case, I can't create the JAVA Objects as we have flexibility in JAXB.
Also, using JAXB, we can create the Classes very easily for Schemas say XSD, WSDL etc..
Those can be very easily used for Objects manipulations.

The reqirement is mainly to change the XML into Java Objects.. that further Can be useful to generate any kind of file, For example, say .xyz using these objects with modification - Giving flexibility.

Could you plz advice anything on above.