VMware Cloud Community
UofS
Enthusiast
Enthusiast
Jump to solution

How to extract an XML attribute

I want to extract the NetworkConnection network name (none) in the following xml content:

<ns6:NetworkConnection network="none" needsCustomization="true" xmlns="http://www.vmware.com/vcloud/versions" xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ns3="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:ns6="http://www.vmware.com/vcloud/v1.5" xmlns:ns7="http://www.vmware.com/schema/ovf" xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1" xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5">

  <ns6:NetworkConnectionIndex>0</ns6:NetworkConnectionIndex>

  <ns6:IsConnected>false</ns6:IsConnected>

  <ns6:MACAddress>00:50:56:3f:03:5c</ns6:MACAddress>

  <ns6:IpAddressAllocationMode>NONE</ns6:IpAddressAllocationMode>

</ns6:NetworkConnection>

I have:

var nsXML = ns.toXml();

    var doc = new XML(nsXML);
    //default xml namespace = doc.namespace();
    var networkName = doc.NetworkConnection.network;;
    System.log(networkName);

and am getting null

I dont know how to reference the attribute.

0 Kudos
1 Solution

Accepted Solutions
maverix7
VMware Employee
VMware Employee
Jump to solution

You can try with

doc.attribute("network")

or

doc.@network

View solution in original post

0 Kudos
2 Replies
maverix7
VMware Employee
VMware Employee
Jump to solution

You can try with

doc.attribute("network")

or

doc.@network
0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

Or alternatively

var networkName = doc["@network"];

I like better the DOM like style doc.attribute("network") as it is easier to read and you can pass special characters.

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