Javascript parsing XML with dot

k_prathip
Participant III

@Anil Sagar

(1) I am trying to parse a SOAP response XML in javascript and is there a way to parse a node name which contains a "." (dot) in the name.

<Test><a.b>123</a.b></Test>

I am trying to use XPath to browse to a node and get it.

(2) Is there a way to call the XML-To-JSON policy from the javascript so that I can get the JSON equivalent of the XML and parse it easily.

(3) Also saw post that E4X kind of parsing will not be supported in rhino newer version, so how soo we have to make the switch,.

1 6 3,468
6 REPLIES 6

  1. is there a way to parse a node name which contains a "." (dot) in the name.

    Yes. I don't know what you mean by "parse" , but for sure you could parse your simple example with a Regex. Does that satisfy? If not, can you be more specific about what you want to do?
  2. Is there a way to call the XML-To-JSON policy from the javascript ?

    No. You cannot do that. You can call the XML-To-JSON policy from the policy flow. Do it before calling your JavaScript. Like this:

        <Flow name="f1">
          <Request>
            <Step><Name>XMLToJSON-1</Name></Step>
            <Step><Name>JS-ParseTheJson</Name></Step>
          </Request>
          <Response>
              ...
          </Response>
    	

    Also, once you've got JSON, you can import all the properties contained within the JSON into context variables. Then just read the appropriate variable; in your case it would be something like this:

    var value = context.getVariable('json_Test_a.b');
    context.setVariable('response.content', 'The value is: ' + value);
    context.setVariable('response.header.content-type', 'text/plain');
    	

    See the attached example working proxy.

    apiproxy.zip

    This doesn't answer exactly what you asked, but it might satisfy your requirement anyway.

  3. E4X parsing will not be supported in Rhino newer version; how soon must we make the switch?

    JS callouts in Apigee are based on the current Rhino and there is no plan to update it. So you do not have to avoid E4X in JS callouts today. But you may wish to avoid E4X for other reasons - it's hard to read and maintain.

@Dino I would have loved to use the XML2JSON but I am making the call from javascript and I get the response in the javascript where I have to read an element from the SOAP response...

I don't understand. You have control over the API Proxy, no? You can change the proxy to just call the XMLToJSON before calling your JS, right?

What's the problem with using XMLToJSON ? You said "I would have loved to use XMLToJSON" as if it is not possible to use it. Why not? I don't get it.

k_prathip
Participant III

The Soap calls has to be made in recursive fashion inside a for loop. So it can be done from the javascript. So the entire processing of the soap response has to happen in the javascript. So that is the reason I am not able to use the XML to Json policy.

ah, ok, I understand.

Have you looked into using the nodejs target for this?

There are myriad modules that convert XML to JSON. For example, https://www.npmjs.com/package/xml-js

You could make short work of calling SOAP requests in a loop and converting them.

If you don't like nodejs you could try this module:

https://github.com/goto100/xpath

It might run in Rhino. I haven't tested it.

ok I just tried this. It worked for me. I used browserify on the npm module xpath (and the dependency xmldom) to invoke xpath from a JS callout. The JS code looks like this:

var xpath = require('xpath'),
    dom = require('xmldom').DOMParser;
var xml = context.getVariable(properties.sourceMessage + ".content"); 
var doc = new dom().parseFromString(xml);
var nodes = xpath.select("//Test/a.b", doc);
context.setVariable(properties.outputVariable, nodes[0].firstChild.data);

And the result is '123' with your test data.

<Test><a.b>123</a.b></Test>

Here's how I did it. (from the terminal)

  1. download the API Proxy bundle. extract it to a directory, say ~/dev/prathip/xpath
  2. Using a text editor, Create a JS policy file in ~/dev/prathip/xpath/apiproxy/policies/JS-ApplyXPath.xml , configured like this:

    <Javascript name='JS-ApplyXPath' timeLimit='200' >
      <Properties>
        <Property name='sourceMessage'>contrivedMessage</Property>
        <Property name='outputVariable'>xpath_extracted</Property>
      </Properties>
      <ResourceURL>jsc://bundle.js</ResourceURL>
    </Javascript>
  3. You will want to insert a reference to that policy somewhere in your proxy endpoint flow.
  4. using a text editor, create a file in ~/dev/prathip/xpath/apiproxy/resources/jsc/getElement.js, with contents as above
  5. open a terminal window, cd to ~/dev/prathip/xpath/apiproxy/resources/jsc
  6. from that terminal, run this command:

    npm install -g browserify
  7. From the terminal, run these additional commands:

    npm install xmldom
    npm install xpath
    browserify getElement.js -o bundle.js

    This packages up your code in such a way that it will be runnable in Rhino (JS Callout). You can verify that the browserify worked by examining the output file called bundle.js.

  8. Remove the node_modules directory from the ~/dev/prathip/xpath/apiproxy/resources/jsc directory
  9. Package up and import and deploy the resulting API Proxy. I use this tool. You can use whatever you like.
  10. Run it.

If you want to make changes to the JS, then modify getElement.js, re-run the steps 7-10 above.

Once you get this working, you can add the calls to the SOAP endpoint. Then re-run steps 7-10.