VMware Cloud Community
ggibson9
VMware Employee
VMware Employee
Jump to solution

XML Plugin-Reading from Nodes with Periods

We have been using the XML plugin to read the response and create requests for REST operations, and it has been working pretty well until some new rest calls.  We are now getting back XML that contains a periods in the node names.  We can’t figure out how to read this.  Here is a simple example, because there is a period between node.one I cannot seem to get the value for ID.  I tried using a \ as an escape character for the period, but vCO throws an exception.

Any help would be greatly appreciated.

contentAsString = <root>

                <node.one>

                                <id>5</id>

                </node.one>

     </root>;

contentAsString = contentAsString.toString();

xmldocument = new XML(contentAsString);

System.log("Total Machine Count: " + xmldocument.node.one.id);

Note: if I take the period out of node.one it will work just fine, but I don't have any control of the XML I'm getting, so I cannot do that.

Reply
0 Kudos
1 Solution

Accepted Solutions
sanchezs
VMware Employee
VMware Employee
Jump to solution

Hi,

You can try with the XMLManager and its related scripting objects, and with this (a little bit more verbose) approach:

var document = XMLManager.fromString(contentAsString).getDocumentElement();
var count = document.getElementsByTagName("node.one").item(0).getElementsByTagName("id").item(0).textContent;
System.log("Total Machine Count: " + count);

The problem with the direct approach is that in the line with the scripting code "xmldocument.node.one.id" it's not possible to say if "one" is part of the node name ("node.one") or it's a property of the node "node" (again "node.one"). The same occurs if instead of a period (".") you have an hyphen ("-") for example, since inside the scripting it would be interpreted as a subtraction of two variables. But if you had an underscore ("_") it should work.

I hope it helps.

Sergio

View solution in original post

Reply
0 Kudos
3 Replies
sanchezs
VMware Employee
VMware Employee
Jump to solution

Hi,

You can try with the XMLManager and its related scripting objects, and with this (a little bit more verbose) approach:

var document = XMLManager.fromString(contentAsString).getDocumentElement();
var count = document.getElementsByTagName("node.one").item(0).getElementsByTagName("id").item(0).textContent;
System.log("Total Machine Count: " + count);

The problem with the direct approach is that in the line with the scripting code "xmldocument.node.one.id" it's not possible to say if "one" is part of the node name ("node.one") or it's a property of the node "node" (again "node.one"). The same occurs if instead of a period (".") you have an hyphen ("-") for example, since inside the scripting it would be interpreted as a subtraction of two variables. But if you had an underscore ("_") it should work.

I hope it helps.

Sergio

Reply
0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

I like to use E4X DOM like capabilities so you get the best of both worlds:

System.log("Total Machine Count: " + xmldocument.child("node.one").id);

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
admin
Immortal
Immortal
Jump to solution

Thanks Sergio. It's an useful piece of information.

Reply
0 Kudos