Highlighted
Contributor
Contributor

Basic XML parsing using E4X syntax

Jump to solution

Hi there,

I'm going crazy with E4X parsing. I'm trying to parse a rather simple XML document but I can't seem to find a way to parse what I need.

I'm trying to extract vApps name (only 1 vApp in this example) :

var xmldoc = new XML(test);

if (!xmldoc) {

  System.debug("XML PAS BON");

  var errorCode = "Invalid XML Document";

  throw "Invalid XML document";

}

System.debug("test 1 : "+xmldoc..AdminVAppRecord.@name);

This is the XML content (a response from vCloud Director 5.1 REST API) :

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

<QueryResultRecords xmlns="http://www.vmware.com/vcloud/v1.5" total="1" pageSize="25" page="1" name="adminVApp" type="application/vnd.vmware.vcloud.query.idrecords+xml" href="https://myvcloudhost.com/api/admin/extension/vapps/query?page=1&pageSize=25&format=idrecords&filter=..." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://myvcloudhost.com/api/v1.5/schema/master.xsd">

  <Link rel="alternate" type="application/vnd.vmware.vcloud.query.references+xml" href="https://myvcloudhost.com/api/admin/extension/vapps/query?page=1&pageSize=25&format=references&filter..."/>

  <Link rel="alternate" type="application/vnd.vmware.vcloud.query.records+xml" href="https://myvcloudhost.com/api/admin/extension/vapps/query?page=1&pageSize=25&format=records&filter=is..."/>

  <AdminVAppRecord vdcName="john-vdc" vdc="urn:vcloud:vdc:4cc245-23f0-439a-9b98-e85319106af1" storageKB="8388608" status="SUSPENDED" ownerName="system" org="urn:vcloud:org:19893ffe-b5de-40a6-9a08-307e41ac0f49" numberOfVMs="1" name="qsdqs" memoryAllocationMB="3072" isVdcEnabled="true" isInMaintenanceMode="false" isExpired="true" isEnabled="true" isDeployed="false" isBusy="false" creationDate="2013-08-02T15:01:39.260+02:00" cpuAllocationMhz="2" id="urn:vcloud:vapp:fe07c653-4848-4f03-a1f8-ddba8da13a50" honorBootOrder="false" pvdcHighestSupportedHardwareVersion="9" cpuAllocationInMhz="4000" taskStatus="success" lowestHardwareVersionInVApp="9" task="urn:vcloud:task:090db9f8-4b04-4550-a1a3-5396c4f25043" autoDeleteDate="2013-08-10T17:47:56.683+02:00" numberOfCpus="2" taskStatusName="vdcUpdateVapp"/>

</QueryResultRecords>

Could someone give me a hand on that one ? I think a working example would be enough for me to start parsing correctly.

Thank you for your help.

1 Solution

Accepted Solutions
Highlighted
VMware Employee
VMware Employee

A couple of errors here:

1. Your XML is not well-formed as it contains non-escaped '&' characters; these should be replaced with '&amp;'

2. Your XML elements are in a namespace (see 'xmlns' attribute), but your E4X query is not. You need to define the namespace, for example

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

and then the query will become

System.log("test 1 : " + xmldoc.vcloud::AdminVAppRecord.@name);

Hope this helps,

-Ilian

View solution in original post

4 Replies
Highlighted
Enthusiast
Enthusiast

Hello,

Try this:

xmldoc.AdminVAppRecord.(@name)

I didn't test this myself, but let me know if it doesn't work for you

0 Kudos
Highlighted
Contributor
Contributor

Same result.

I also tried with double dots : xmldoc..AdminVAppRecord.(@name)

0 Kudos
Highlighted
VMware Employee
VMware Employee

A couple of errors here:

1. Your XML is not well-formed as it contains non-escaped '&' characters; these should be replaced with '&amp;'

2. Your XML elements are in a namespace (see 'xmlns' attribute), but your E4X query is not. You need to define the namespace, for example

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

and then the query will become

System.log("test 1 : " + xmldoc.vcloud::AdminVAppRecord.@name);

Hope this helps,

-Ilian

View solution in original post

Highlighted
Contributor
Contributor

Thanks a lot for your help.

I had some help to code a parsing function but this is way easier with the E4X method !

It's working as it should after setting the namespace. I thought the namespace was always before the tagname (as in <vmext:network title="test"/>) but I'm not expert when it comes to XML syntax 🙂

0 Kudos