How to Traverse XML hierarchy using .asXML

Not applicable

Hi All,

This is continuation to question https://community.apigee.com/questions/2221/can-apigee-asxml-objects-support-colons-in-xml-tag.html

i'm trying to read the request xml, format is below

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <login xmlns="urn:xxobject.xxx.xxxx.com" xmlns:ns2="urn:fault.xxx.xxxx.com">
      <credential>
        <cId>1234</cId>
        <username>user</username>
        <password>pass</password>
      </credential>
      <param>
        <name>batchSize</name>
        <value>200</value>
      </param>
    </login>
  </S:Body>
</S:Envelope>

i would like to access element <cId>1234</cId> in above xml, could you let me know how to achieve it in javascript of apigee. i tried below options, however it didn't work

option:1

var xmlObj = request.content.asXML;
var S = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var value = xmlObj.S::Body.credential.cId.toString();
context.setVariable("value1",context.getVariable('value'));

option-2

var xmlObj = request.content.asXML;
var S = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var l= new Namespace ('urn:xxobject.sxxx.xxxx.com');
var value = xmlObj.S::Body.l::credential.companyId.toString();
context.setVariable("value1",context.getVariable('value'));

Regards,

Ch.Venkat

Solved Solved
1 4 768
1 ACCEPTED SOLUTION

Venkat, you want this:

var xmlObj = request.content.asXML;
var S = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var ns1 = new Namespace('urn:xxobject.xxx.xxxx.com');
var value1 = xmlObj.S::Body.ns1::login.ns1::credential.ns1::cId.toString();
context.setVariable("name",value1);

Notice that I have included the login element . It is the parent of the credential element. You need to include it.

View solution in original post

4 REPLIES 4

I haven't tried this, but your code should be like this

> context.setVariable("value1",value);

if this was working, 'value1' in the context will have the javascript variable 'value'

Venkat, it seems you have given up on the ExtractVariables approach with the proper Xpath and namespace settings?

@Dino : yes, i'm using extract variable policy. However if you have any solution for above, please post here.

Venkat, you want this:

var xmlObj = request.content.asXML;
var S = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var ns1 = new Namespace('urn:xxobject.xxx.xxxx.com');
var value1 = xmlObj.S::Body.ns1::login.ns1::credential.ns1::cId.toString();
context.setVariable("name",value1);

Notice that I have included the login element . It is the parent of the credential element. You need to include it.