Unable to Parse javascript WebService Response asXML

Hello, I am trying to parse XML response after hitting a service from within a javascript policy, I am able to print the response content as string but unable to parse it as asXML.element... what am i missing???

// Get the request as JSON


var InputRoot_JSON = request.content.asJSON;
var array = InputRoot_JSON.AashihsCL;
var array_length = array.length; 




var XML = new XMLWriter();
XML.BeginNodeNs("PersonalInfo","per")
XML.Attrib("xmlns:per", "http://www.w3.org/TR/html4/");


    
XML.CreateElement("Aashish",InputRoot_JSON.Aashish)
   
    XML.BeginNode("ClassmateNames"); 
        for(var i=0; i<array_length; i++)
        { 
            XML.CreateElement("ClassmateName",array[i]);
        }
    XML.EndNode();
    XML.EndNode();
XML.EndNode();


//----------------------------------------------------------------------------------
var headers = {
                 'Authorization' : 'Bearer-Aashish-Kotagiri',
                 'Content-Type'  : 'application/xml'
             };


 var req = new Request('http://mockbin.org/bin/e928616f-d5ba-4da9-ac00-777fe4aaba23',
                        'POST',
                        headers,
                        XML.ToString());
                        
  var exchange = httpClient.send(req);


  // Wait for the asynchronous POST request to finish
  exchange.waitForComplete();


  if (exchange.isSuccess()) {


        var name1 = exchange.getResponse().content;
         print(name1);
	// Prints the response xml
	

        
        var root = exchange.getResponse().content.asXML.book[1].title;
      	print(root);
	// fails with invalid object unable to access book.

    }


   else if (exchange.isError()) {
    throw new Error(exchange.getError());
  }
    
    
    
        
// Webservice Callout
      
context.setVariable('Environment.XMLNSC',XML.ToString ());
0 3 637
3 REPLIES 3

Check the Content-type header in the response. I believe it should be application/xml for the asXML to work properly.

Yes the response type is application/xml

"mimeType": "application/xml",
"text": "<customer number='1'>\n<name>Fred<name/>\n<customer/>",

You can hit this service with SOAP UI and get response

http://mockbin.org/bin/e928616f-d5ba-4da9-ac00-777fe4aaba23/view