Sunday, September 12, 2010

How to create a XmlObject using XmlCursor

String GEONS="http://www.acme.com/schema/GeoServicePS/v1";
String operationResponse="getCountriesResponse";

XmlObject result = XmlObject.Factory.newInstance();
XmlCursor cursor = result.newCursor();
QName responseQName = new QName(GEONS, operationResponse);
cursor.toNextToken();
cursor.beginElement(responseQName);
for (Location loc : locations) {
 cursor.beginElement(new QName(GEONS, "Location"));
 cursor.insertElementWithText(new QName(GEONS, "countryid"), loc.getCountryId());
 cursor.toParent();
}




this will produce:



ITAU





If you want to extract the operation name from the $body (an XmlObject), this is how:


XmlCursor newCursor = body.newCursor();
newCursor.toFirstChild();
  
System.out.println("operation=" + newCursor.getName().getLocalPart());


If you want to extract the values of the Operation parameters, you can use the XmlCursor APIs:
toFirstChild()
getTextValue()
toNextSibling()

No comments: