VMware Cloud Community
DanyG01
Enthusiast
Enthusiast
Jump to solution

xml in vCO

Hi,

  I need help on how to deal with xml in vCO.

How can I get the 'name' and corresponding 'objectId' of each 'virtualWire' from the xml below?

    <?xml version="1.0" encoding="UTF-8"?>

    <virtualWires>

    <dataPage>

      <pagingInfo>

        <pageSize>20</pageSize>

        <startIndex>0</startIndex>

        <totalCount>27</totalCount>

        <sortOrderAscending>true</sortOrderAscending>

      </pagingInfo>

      <virtualWire>

        <objectId>virtualwire-5</objectId>

        <objectTypeName>VirtualWire</objectTypeName>

        <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

        <revision>2</revision>

        <type>

          <typeName>VirtualWire</typeName>

        </type>

        <name>EG01</name>

        <description>

        </description>

        <clientHandle>

        </clientHandle>

        <extendedAttributes/>

        <tenantId>virtual wire tenant</tenantId>

        <vdnScopeId>vdnscope-2</vdnScopeId>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-473</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>166</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsEDGE</name>

            <scope>

              <id>group-n464</id>

              <objectTypeName>Folder</objectTypeName>

              <name>EDGE</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-4838</backingValue>

        </vdsContextWithBacking>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-476</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>169</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsNUA</name>

            <scope>

              <id>group-n466</id>

              <objectTypeName>Folder</objectTypeName>

              <name>NUA</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-4837</backingValue>

        </vdsContextWithBacking>

        <vdnId>5002</vdnId>

        <multicastAddr>239.196.0.101</multicastAddr>

        <guestVlanAllowed>false</guestVlanAllowed>

        <controlPlaneMode>MULTICAST_MODE</controlPlaneMode>

      </virtualWire>

      <virtualWire>

        <objectId>virtualwire-6</objectId>

        <objectTypeName>VirtualWire</objectTypeName>

        <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

        <revision>2</revision>

        <type>

          <typeName>VirtualWire</typeName>

        </type>

        <name>EG02</name>

        <description>

        </description>

        <clientHandle>

        </clientHandle>

        <extendedAttributes/>

        <tenantId>e7aee317-824f-4277-b323-7751974da420</tenantId>

        <vdnScopeId>vdnscope-2</vdnScopeId>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-473</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>166</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsEDGE</name>

            <scope>

              <id>group-n464</id>

              <objectTypeName>Folder</objectTypeName>

              <name>EDGE</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-5202</backingValue>

        </vdsContextWithBacking>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-476</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>169</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsNUA</name>

            <scope>

              <id>group-n466</id>

              <objectTypeName>Folder</objectTypeName>

              <name>NUA</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-5201</backingValue>

        </vdsContextWithBacking>

        <vdnId>5003</vdnId>

        <multicastAddr>239.196.0.100</multicastAddr>

        <guestVlanAllowed>false</guestVlanAllowed>

        <controlPlaneMode>MULTICAST_MODE</controlPlaneMode>

      </virtualWire>

    </dataPage>

    </virtualWires>

Thanks

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

OK, if the XML is not hard-coded as it was on your first example, then the syntax is a little bit different.

var request = nsx_host.createRequest("GET", "/2.0/vdn/virtualwires", null);

var strBasicAuthentication;

strBasicAuthentication = "***";

request.setHeader("Accept","application/*+xml;version=5.6");

request.setHeader("Content-Type","application/xml");

request.setHeader("Authorization","Basic " + strBasicAuthentication);

var response = request.execute();

org_list = response.contentAsString;

var doc = new XML(org_list); // construct XML document from dynamic string value

for each (var wire in doc.dataPage.virtualWire) {

  System.log("wire --> objectId: " + wire.objectId + " name: " + wire.name);

}

View solution in original post

0 Kudos
5 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The easiest way to work with XML in Javascript is via E4X.

Here is some sample code showing how to go get name and objectId from your XML:

var doc = ...; // replace ... with your long XML content

for each (var wire in doc.dataPage.virtualWire) {

  System.log("wire --> objectId: " + wire.objectId + " name: " + wire.name);

}

DBA_ONE
Contributor
Contributor
Jump to solution

If you are attempting to do this in SQL start with this:

DECLARE    @ParseMe  XML='<?xml version="1.0" encoding="UTF-8"?>

    <virtualWires>

    <dataPage>

      <pagingInfo>

        <pageSize>20</pageSize>

        <startIndex>0</startIndex>

        <totalCount>27</totalCount>

        <sortOrderAscending>true</sortOrderAscending>

      </pagingInfo>

      <virtualWire>

        <objectId>virtualwire-5</objectId>

        <objectTypeName>VirtualWire</objectTypeName>

        <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

        <revision>2</revision>

        <type>

          <typeName>VirtualWire</typeName>

        </type>

        <name>EG01</name>

        <description>

        </description>

        <clientHandle>

        </clientHandle>

        <extendedAttributes/>

        <tenantId>virtual wire tenant</tenantId>

        <vdnScopeId>vdnscope-2</vdnScopeId>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-473</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>166</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsEDGE</name>

            <scope>

              <id>group-n464</id>

              <objectTypeName>Folder</objectTypeName>

              <name>EDGE</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-4838</backingValue>

        </vdsContextWithBacking>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-476</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>169</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsNUA</name>

            <scope>

              <id>group-n466</id>

              <objectTypeName>Folder</objectTypeName>

              <name>NUA</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-4837</backingValue>

        </vdsContextWithBacking>

        <vdnId>5002</vdnId>

        <multicastAddr>239.196.0.101</multicastAddr>

        <guestVlanAllowed>false</guestVlanAllowed>

        <controlPlaneMode>MULTICAST_MODE</controlPlaneMode>

      </virtualWire>

      <virtualWire>

        <objectId>virtualwire-6</objectId>

        <objectTypeName>VirtualWire</objectTypeName>

        <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

        <revision>2</revision>

        <type>

          <typeName>VirtualWire</typeName>

        </type>

        <name>EG02</name>

        <description>

        </description>

        <clientHandle>

        </clientHandle>

        <extendedAttributes/>

        <tenantId>e7aee317-824f-4277-b323-7751974da420</tenantId>

        <vdnScopeId>vdnscope-2</vdnScopeId>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-473</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>166</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsEDGE</name>

            <scope>

              <id>group-n464</id>

              <objectTypeName>Folder</objectTypeName>

              <name>EDGE</name>

            </scope>

            <clientHandle>

            </clientHandle>

            <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-5202</backingValue>

        </vdsContextWithBacking>

        <vdsContextWithBacking>

          <switch>

            <objectId>dvs-476</objectId>

            <objectTypeName>VmwareDistributedVirtualSwitch</objectTypeName>

            <vsmUuid>420B229E-9DC2-8FE9-8AA1-DE7ADCCFB0</vsmUuid>

            <revision>169</revision>

            <type>

              <typeName>VmwareDistributedVirtualSwitch</typeName>

            </type>

            <name>vdsNUA</name>

            <scope>

              <id>group-n466</id>

              <objectTypeName>Folder</objectTypeName>

              <name>NUA</name>

            </scope>

            <clientHandle>

            </clientHandle>

           <extendedAttributes/>

          </switch>

          <mtu>9000</mtu>

          <promiscuousMode>false</promiscuousMode>

          <backingType>portgroup</backingType>

          <backingValue>dvportgroup-5201</backingValue>

        </vdsContextWithBacking>

        <vdnId>5003</vdnId>

        <multicastAddr>239.196.0.100</multicastAddr>

        <guestVlanAllowed>false</guestVlanAllowed>

        <controlPlaneMode>MULTICAST_MODE</controlPlaneMode>

      </virtualWire>

    </dataPage>

    </virtualWires>'

select

    objectid=A.value('objectId[1]', 'varchar(100)')

    ,WireName=A.value('objectTypeName[1]', 'varchar(100)')

    FROM @ParseMe.nodes('virtualWires/dataPage/virtualWire') xmlData(A)

0 Kudos
DanyG01
Enthusiast
Enthusiast
Jump to solution

Hi,

  Thanks for your help.

I tried your sample code and I got this error

[2015-09-14 15:27:48.482] [I] TypeError: Cannot read property "virtualWire" from undefined (Workflow:NSX PortgroupID / List organizations (item0)#13)

Here is my sample code:

var request = nsx_host.createRequest("GET", "/2.0/vdn/virtualwires", null);

var strBasicAuthentication;

strBasicAuthentication = "***";

request.setHeader("Accept","application/*+xml;version=5.6");

request.setHeader("Content-Type","application/xml");

request.setHeader("Authorization","Basic " + strBasicAuthentication);

var response = request.execute();

org_list = response.contentAsString;

for each (var wire in org_list.dataPage.virtualWire) {

  System.log("wire --> objectId: " + wire.objectId + " name: " + wire.name);

}

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

OK, if the XML is not hard-coded as it was on your first example, then the syntax is a little bit different.

var request = nsx_host.createRequest("GET", "/2.0/vdn/virtualwires", null);

var strBasicAuthentication;

strBasicAuthentication = "***";

request.setHeader("Accept","application/*+xml;version=5.6");

request.setHeader("Content-Type","application/xml");

request.setHeader("Authorization","Basic " + strBasicAuthentication);

var response = request.execute();

org_list = response.contentAsString;

var doc = new XML(org_list); // construct XML document from dynamic string value

for each (var wire in doc.dataPage.virtualWire) {

  System.log("wire --> objectId: " + wire.objectId + " name: " + wire.name);

}

0 Kudos
DanyG01
Enthusiast
Enthusiast
Jump to solution

Thank you, thank you !!! Smiley Happy

0 Kudos