Matthias' Blog

Custom XML objects in vRealize Orchestrator

Lately I had the issue that I needed to create custom XML objects based on variables. In first place it sounds simple, but in the end, there are a couple of different methods doing this. This article will discuss these options.

The first method is a simple string, but if the object gets a bit larger, this is not very useful.

var xmlString = “<section></section>”;

The advantage of this method is, that the string can be directly used within a REST call but converting an XML object into a string is not rocket science.

The second method is creating the XML object directly in the code.

var xmlObject =

For the sufficiency of this article, this can be done with the following line of code. Be aware, before converting an object into a string, we need to create the object.

var xmlString = xmlObject.toString();

Now that we’ve covered creating really basic XML objects and/or strings, let’s look into a more complex XML structure. For the examples I am going to use the firewall ruleset XML of the distributed firewall of NSX-v. I’ve chosen this example because firewall rules are highly dynamic and sometimes you need just a few entries and sometimes maybe 50 rules or even more.

Of course, using E4X, enables us to easy add information to the XML object, but to add information, the part of the XML object needs to exist.

Before we start adding dynamic content, we need to build a base object for the rules which is identical for each rule and of course the section XML object. That can be built more dynamic but for this example we start with some basic content. The section object was shown before, even though, more information for the section is needed, but that is part of the NSX documentation.

var rule =
      <sources excluded=“false“></sources>
      <destinations excluded=“false></destinations>

Now we have the basic rule object and we start adding dynamic content. To automate the firewall ruleset creation, I suggest creating a custom object to loop through. For each entry in this object, a new rule can be created but therefor, before doing the REST call, we need to dynamically add additional entries to our XML object. Therefor we use the rule XML object we created, fill it with data and add it to our section object. Let’s do an example based on the rule object:

var destination =
destination.value = << here goes the looping object >>;

This is an example for the destination. Objects for source and services are built the exact same way. Now that we have created the dynamic content, and imagine we did the same for source and service, we need to add these to the rule using the following method:

rule.destinations.destination += destination;
rule.sources.source += source; += service;

The above code block adds the corresponding newly created XML object to the previously built XML object at the right places within the rule object. The last step is to add the newly dynamic built rule to the main object for the distributed firewall ruleset creation.

section.rule += rule;

I hope this helps giving you guys an idea how to dynamically create XML objects in JavaScript and vRealize Orchestrator.