The first method is a simple string, but if the object gets a bit larger, this is not very useful.
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.
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.
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.
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:
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.