java - XMLStreamReader and UnMarshalling a SOAP Message -
i have problem while decoding soap envelope. here xml
<?xml version="1.0"?> <env:envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:tns="http://c.com/partner/"> <env:header>c <tns:messageid env:mustunderstand="true">3</tns:messageid> </env:header> <env:body> <getforkliftpositionresponse xmlns="http://www.c.com"> <forkliftid>pc006</forkliftid> </getforkliftpositionresponse> </env:body> </env:envelope>
i use following code decode body, return namespace tns:messageid, not env:body. convert xmlstreamreader string debugging issues, possible?
xmlinputfactory xif = xmlinputfactory.newfactory(); xif.setproperty("javax.xml.stream.iscoalescing", true); // decode entities 1 string stringreader reader = new stringreader(message); string soapbody = ""; xmlstreamreader xsr = xif.createxmlstreamreader( reader ); xsr.nexttag(); // advance header tag xsr.nexttag(); // advance envelope xsr.nexttag(); // advance body
initially xsr pointing before document event (i.e. xml declaration), , nexttag()
advances next tag, not next sibling element:
xsr.nexttag(); // advance opening envelope tag xsr.nexttag(); // advance opening header tag xsr.nexttag(); // advance opening messageid
if want skip body better idiom be
boolean foundbody = false; while(!foundbody && xsr.hasnext()) { if(xsr.next() == xmlstreamconstants.start_element && "http://www.w3.org/2003/05/soap-envelope".equals(xsr.getnamespaceuri()) && "body".equals(xsr.getlocalname())) { foundbody = true; } } // if foundbody == true, xsr pointing opening body tag. // if foundbody == false, ran out of document before finding body if(foundbody) { // advance next tag - either opening tag of // element inside body, if there one, or closing body tag if // there isn't if(xsr.nexttag() == xmlstreamconstants.start_element) { // pointing @ opening tag of getforkliftpositionresponse } else { // pointing @ </env:body> - body empty } }
Comments
Post a Comment