VMware Cloud Community
shaharsib
Enthusiast
Enthusiast

XML parse

Hi,

i got xml object (that i had lots for trouble load)

I used Basic XML parsing using E4X syntax to help me load.

the xml comes from HTTP request from my vcloud director using the vcd plugin for vro.

i want to retrieve all the names from the xml file, but everytime i tried it keep printing the root variable

any help ?

here is the xml im working on (there might be syntax error now because i edited it - but the origin is good)

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

<AdminCatalog xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:common="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vmw="http://www.vmware.com/schema/ovf" xmlns:ovfenv="http://schemas.dmtf.org/ovf/environment/1" xmlns:vmext="http://www.vmware.com/vcloud/extension/v1.5" xmlns:ns9="http://www.vmware.com/vcloud/versions" name="CD_Catalog" id="urn:vcloud:catalog:4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e" href="https://vcloud-url/api/admin/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/vnd.vmware.admin.catalog+x...">

    <Link rel="up" href="https://vcloud-url/api/admin/org/5741ed36-8089-41d9-b92e-2ccbfd6e4401/vnd.vmware.admin.organization+..."/>

    <Link rel="publish" href="https://vcloud-url/api/admin/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/action/publish/vnd.vmware...."/>

    <Link rel="publishToExternalOrganizations" href="https://vcloud-url/api/admin/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/action/publishToExternalOr..."/>

    <Link rel="subscribeToExternalCatalog" href="https://vcloud-url/api/admin/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/action/subscribeToExternal..."/>

    <Link rel="down" href="https://vcloud-url/api/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/controlAccess/vnd.vmware.vcloud...."/>

    <Link rel="controlAccess" href="https://vcloud-url/api/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/action/controlAccess/vnd.vmware...."/>

    <Link rel="down" href="https://vcloud-url/api/admin/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/extension/vnd.vmware.admin..."/>

    <Link rel="alternate" href="https://vcloud-url/api/admin/catalog/4ec3a2e7-8aab-4c25-aac7-30d0b107cc2e/vnd.vmware.admin.catalog+x..."/>

    <Description></Description>

    <Owner type="application/vnd.vmware.vcloud.owner+xml">

        <User href="https://vcloud-url/api/admin/user/3f2edf0a-0bff-4c11-a746-fb8090009732" name="system/vnd.vmware.admin.user+xml"/>

    </Owner>

    <CatalogItems>

        <CatalogItem href="https://vcloud-url/api/catalogItem/0761bd5d-011d-4780-9c86-2b5082b0e600" id="0761bd5d-011d-4780-9c86-2b5082b0e600" name="some_template_name type="application/vnd.vmware.vcloud.catalogItem+xml"/>

        <CatalogItem href="https://vcloud-url/api/catalogItem/0a02946f-4350-47fb-9842-1c0d4dbd4d22" id="0a02946f-4350-47fb-9842-1c0d4dbd4d22" name="some_template_name type="application/vnd.vmware.vcloud.catalogItem+xml"/>

        <CatalogItem href="https://vcloud-url/api/catalogItem/0e84f9f4-85ba-4cec-8969-a7dfc5693d93" id="0e84f9f4-85ba-4cec-8969-a7dfc5693d93" name="some_template_name

        /vnd.vmware.vcloud.catalogItem+xml"/>

        <CatalogItem href="https://vcloud-url/api/catalogItem/e698719c-4cf8-4927-b2cd-3e23b43105ef" id="e698719c-4cf8-4927-b2cd-3e23b43105ef" name="some_template_name                             /vnd.vmware.vcloud.catalogItem+xml"/>

        <CatalogItem href="https://vcloud-url/api/catalogItem/fce6156b-31a9-4ac5-9e1b-8d15292f2559" id="fce6156b-31a9-4ac5-9e1b-8d15292f2559" name="some_template_name                    /vnd.vmware.vcloud.catalogItem+xml"/>

    </CatalogItems>                                                                                                                                                    

    <IsPublished>true</IsPublished>                                                                                                                                    

    <DateCreated>2018-11-05T14:03:59.573Z</DateCreated>                                                                                                                

    <VersionNumber>3504</VersionNumber>

    <CatalogStorageProfiles>

        <VdcStorageProfile href="https://vcloud-url/api/admin/vdcStorageProfile/0f44ed6d-824b-4d5c-b9ee-7c721c43da5f" id="urn:vcloud:vdcstorageProfile:0f44ed6d-824b-4d5c-b9ee-7c721c43da5f" name="" type="com.vmware.vcloud.entity.vdcstorageProfile"/>

    </CatalogStorageProfiles>

</AdminCatalog>

5 Replies
lurims
Enthusiast
Enthusiast

While this can be done in XML parsing too, I would convert this to JSON using xml2json object (search for xml2json in the object explorer).  Try it, it will be very simple with that.  Just make sure you identifiy an array element and non-array elements (square brackets [ ] for array elements and curly braces { } for single property/value in the object.

Example xml object:

<CATALOG>

<CD>

<TITLE>Empire Burlesque</TITLE>

<ARTIST>Bob Dylan</ARTIST>

<COUNTRY>USA</COUNTRY>

<COMPANY>Columbia</COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>

</CD>

<CD>

<TITLE>Hide your heart</TITLE>

<ARTIST>Bonnie Tyler</ARTIST>

<COUNTRY>UK</COUNTRY>

<COMPANY>CBS Records</COMPANY>

<PRICE>9.90</PRICE>

<YEAR>1988</YEAR>

</CD>

</CATALOG>

{

  "CATALOG": {

    "CD": [

      {

        "TITLE": "Empire Burlesque",

        "ARTIST": "Bob Dylan",

        "COUNTRY": "USA",

        "COMPANY": "Columbia",

        "PRICE": "10.90",

        "YEAR": "1985"

      },

      {

        "TITLE": "Hide your heart",

        "ARTIST": "Bonnie Tyler",

        "COUNTRY": "UK",

        "COMPANY": "CBS Records",

        "PRICE": "9.90",

        "YEAR": "1988"

      }

    ]

  }

}

//input object - myXMlObject (above sample xml object vaules are assumed)

var jsonObj = xml2json(myXMlObject) // This wil be your "CATALOG" object

System.log(jsonObj.CD) // This will give the "CD" child object consisting of 2 array elements

System.log(jsonObj.CD[0]) // This wil be the first Array element object in the CDs.

System.log(jsonObj.CD[0].TITLE // This will print "Empire Burlesque"

Note that object property names are case sensitive.

iiliev
VMware Employee
VMware Employee

Hi,

You haven't specified which 'names' do you want to retrieve. Assuming that you are interested of the values of attribute name of the <CatalogItem/> elements (that have values some_template_name in your sample XML), the following code will retrieve and print them:

var xmlstring = ...; // the XML content string that you got from vCloud API call

var xmldoc = new XML(xmlstring);

var ns = new Namespace("http://www.vmware.com/vcloud/v1.5");

var catalogItemNames = xmldoc.ns::CatalogItems.ns::CatalogItem.@name;

for each (var name in catalogItemNames) {

  System.log("CatalogItem name: " + name);

}

If you are interested in other attributes of other elements, just change the E4X expression on line# 4 above.

shaharsib
Enthusiast
Enthusiast

Hi lurims

I tried what you suggested but it didnt work.

It kept printing empty objects.

but it solved now (:

Reply
0 Kudos
shaharsib
Enthusiast
Enthusiast

That solved my problem! thanks!

can you explain to me about this xml.ns::CatalogItems.ns::CatalogItem.@name; path ?

thanks!

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee

OK, the different parts of xmldoc.ns::CatalogItems.ns::CatalogItem.@name expression are:

xmldoc - the variable containing the XML document (line# 2)

ns::SomeType - denotes that SomeType is defined inside the namespace ns. All namespaces for the XML document are defined in the root element, in this case they are

xmlns="http://www.vmware.com/vcloud/v1.5" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" ...

This means that XML elements that have no explicit namespace will be in namespace "http://www.vmware.com/vcloud/v1.5", elements in ovf: namespace will be in "http://schemas.dmtf.org/ovf/envelope/1" , etc.

ns::CatalogItems.ns::CatalogItem - the path to our XML element, starting from, but not including, the root element <AdminCatalog/>

@name​ - the XML attribute of the XML element that we want to collect.