VMware Cloud Community
legioon
Enthusiast
Enthusiast
Jump to solution

Add and remove element in XML

Hi all,

I have a problems about vCO XML plugin; I think the plugin is not enough for some operations.

xml.png

My code is here ;

var document = XMLManager.fromString(xml) ;
var nodeList = document.getElementsByTagName("IPs");


for (var i = 0 ; i < nodeList.length ; i++) {

  newElement = document.createElement("Range");
  nodeList.item(i).appendChild(newElement);
  XMLManager.saveDocument(document,path+"/test.xml") ;


}

you can see result of my code at below ;

xml2.png

Tags (2)
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The plug-in is enough, but there are some issues with your code:

- you don't have code to remove the old Range elements

- you don't have ode to populate content of the new Range elements

Here is some code to demonstrate how to remove the old Range elements and insert a new Range element with some sample content:

var document = XMLManager.fromString(xml) ;

var nodeList = document.getElementsByTagName("IPs");

for (var i = 0 ; i < nodeList.length ; i++) {

  var children = nodeList.item(i).getElementsByTagName("Range");

  // Remove Range elements under IPs

  while (children.length > 0) {

    nodeList.item(i).removeChild(children.item(0));

  }

  // Insert a new Range element with some content

  newElement = document.createElement("Range");

  newStart = document.createElement("Start");

  newStart.textContent = "y.y.y.y";

  newElement.appendChild(newStart);

  newEnd = document.createElement("End");

  newEnd.textContent = "z.z.z.z";

  newElement.appendChild(newEnd);

  nodeList.item(i).appendChild(newElement);

}

XMLManager.saveDocument(document,path+"/test.xml") ;

View solution in original post

4 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The plug-in is enough, but there are some issues with your code:

- you don't have code to remove the old Range elements

- you don't have ode to populate content of the new Range elements

Here is some code to demonstrate how to remove the old Range elements and insert a new Range element with some sample content:

var document = XMLManager.fromString(xml) ;

var nodeList = document.getElementsByTagName("IPs");

for (var i = 0 ; i < nodeList.length ; i++) {

  var children = nodeList.item(i).getElementsByTagName("Range");

  // Remove Range elements under IPs

  while (children.length > 0) {

    nodeList.item(i).removeChild(children.item(0));

  }

  // Insert a new Range element with some content

  newElement = document.createElement("Range");

  newStart = document.createElement("Start");

  newStart.textContent = "y.y.y.y";

  newElement.appendChild(newStart);

  newEnd = document.createElement("End");

  newEnd.textContent = "z.z.z.z";

  newElement.appendChild(newEnd);

  nodeList.item(i).appendChild(newElement);

}

XMLManager.saveDocument(document,path+"/test.xml") ;

legioon
Enthusiast
Enthusiast
Jump to solution

Hi Ilian

You are absolutely wonderful ! you fixed my big problem !! it works...

Well, I have two question to you ;

1. how to get output all xml after changes. I want to push it to string variable.

2. When the last <Range> element was removed, how to remove <IPs> element. It will be appears like this ;

            <Subnets>

                <Gateway>2.2.2.2</Gateway>

                <Netmask>255.255.255.0</Netmask>

                <IpAddress>2.2.2.1</IpAddress>             

            </Subnets>

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

To get the string representation of XML document you can use something like:

var docAsString = XMLManager.getDocumentContent(document);

On the second question, the principle is the same - when you want to remove a node, you need to find its parent node, and then remove the child node using removeChild() method.

For your samle XML, it will be something like:

var nodeList = document.getElementsByTagName("IPs");

document.getElementsByTagName("Subnets").item(0).removeChild(nodeList.item(0));

Reply
0 Kudos
legioon
Enthusiast
Enthusiast
Jump to solution

Hi Ilian,

It works again ! Thanks you so much for your helps, you saved me from a big problem.

i added i little changes to code ;

RangeCout = document.getElementsByTagName("Range").length; 

if (RangeCout==0) { // if there is no any <Range> element, remove IPs Element.

     var nodeList = document.getElementsByTagName("IPs");

     document.getElementsByTagName("Subnets").item(0).removeChild(nodeList.item(0));

}

Thanks you very much again !

Reply
0 Kudos