Quick intro guide: Standard Module "XML"

It seems to me that JSON is supplanting XML in RPA, but XML is not going away anytime soon.
In particular, it seems to me the XPath is great for traversing the DOM and finding elements by text values.

Here I’m building on the results of some code that @Suniel posted for retrieving data from Web APIs. Rather than retrieve the data for each run of the script, I’ve just saved it to a file and edited it to include one attribute for Bangalore,

<summary size="Big">

to illustrate how to work with elements and their attributes. Although w3schools.com says:

Avoid XML Attributes?

Some things to consider when using attributes are:

    attributes cannot contain multiple values (elements can)
    attributes cannot contain tree structures (elements can)
    attributes are not easily expandable (for future changes)

we’re not really going to have control over what others choose to use.

Below is a screenshot of some code that may incite you to use the Robin “XML” module and XPath.

Code itself:

System.GetEnvironmentVariable   Name: 'USERNAME'  Value=> Username


set workDir to 'C:\Users\\'+Username+'\Documents\Robin\9.2\web_api'


# A test file to read.
XML.ReadFromFile                File: workDir + "\XpathTest.xml" \ 
                                Encoding:XML.FileEncoding.DefaultEncoding \
                                XmlDocument=> XmlDocument

# The result here will include the tag.
XML.ExecuteXPath                XmlDocument:  XmlDocument \
                                XPathQuery:  '/geonames/entry[title="Bangalore"]/summary'\
                                XPathResults=> XPathResults

# A demo of the "contains" approach for element value.
XML.GetElementValue             Document:  Xmldocument \
                                XPathQuery:  '/geonames/entry/summary[contains(@size,"B")]'\
                                TextValue=> ContainsTextValue

# A demo of the "starts-with" approach for element value.
XML.GetElementValue             Document:  Xmldocument \
                                XPathQuery:  '/geonames/entry/summary[starts-with(@size,"B")]'\
                                TextValue=> StartsWithTextValue

# Wildcard demo
XML.GetElementValue             Document:  Xmldocument \
                                XPathQuery:  '//*[contains(@size,"B")]'\
                                TextValue=> AnyChildContainsTextValue

# Find the text value of the "size" attribute
XML.GetElementAttribute         Document:  Xmldocument\
                                XPathQuery: '/geonames/entry/summary' \
                                AttributeName: 'size' \
                                TextValue=> AttrValue

# Write out our results below. 
Console.Write                   Message: XPathResults

# Next two give the same result.
Console.Write                   Message: ContainsTextValue

Console.Write                   Message: StartsWithTextValue

# You probably knew this without being told.
Console.Write                   Message: AttrValue

# Once again, brought to you by your local Dept. of Redundancy Dept.:
Console.Write                   Message: AnyChildContainsTextValue

Here is the content of the file “XPathTest.xml”:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<geonames>
    <entry>
        <lang>en</lang>
        <title>Bangalore</title>
        <summary size="Big">Bangalore , officially known as Bengaluru , is the capital of the Indian state of Karnataka. It has a population of over ten million, making it a megacity and the third most populous city and fifth most populous urban agglomeration in India. It is located in southern India on the Deccan Plateau (...)</summary>
        <feature>city</feature>
        <countryCode>IN</countryCode>
        <elevation>926</elevation>
        <lat>12.9779</lat>
        <lng>77.5925</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Bangalore</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/62000/thumb-61337-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Salem, Tamil Nadu</title>
        <summary>Salem (pronunciation) is a city in Salem district in the Indian state of Tamil Nadu. It is located about northeast of Coimbatore, southeast of Karnataka state capital Bangalore and about southwest of the state capital, Chennai. Salem is the fifth largest city in Tamil Nadu by population and covers  (...)</summary>
        <feature/>
        <countryCode>IN</countryCode>
        <elevation>279</elevation>
        <lat>11.6512</lat>
        <lng>78.1587</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Salem%2C_Tamil_Nadu</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/151000/thumb-150448-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Mysore</title>
        <summary>Mysore , officially Mysuru, is a city in the state of Karnataka, India. It is located in the foothills of the Chamundi Hills about towards the southwest of Bangalore and spread across an area of . Mysore City Corporation is responsible for the civic administration of the city, which is also the (...)</summary>
        <feature>city</feature>
        <countryCode>IN</countryCode>
        <elevation>746</elevation>
        <lat>12.3035</lat>
        <lng>76.6461</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Mysore</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/95000/thumb-94820-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Bangalore Urban district</title>
        <summary>Bangalore Urban is a district of the Indian state of Karnataka. It is surrounded by the Bangalore Rural district on the east and north, the Ramanagara district on the west and the Krishnagiri district of Tamil Nadu on the south (...)</summary>
        <feature/>
        <countryCode>IN</countryCode>
        <elevation>876</elevation>
        <lat>12.9702</lat>
        <lng>77.5603</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Bangalore_Urban_district</wikipediaUrl>
        <thumbnailImg/>
        <rank>98</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Badami</title>
        <summary>Badami, formerly known as Vatapi, is a town and headquarters of a taluk by the same name, in the Bagalkot district of Karnataka, India. It was the regal capital of the Badami Chalukyas from AD 540 to 757. It is famous for its rock cut structural temples (...)</summary>
        <feature/>
        <countryCode>IN</countryCode>
        <elevation>560</elevation>
        <lat>15.9149</lat>
        <lng>75.6768</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Badami</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/78000/thumb-77164-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Vellore</title>
        <summary>Vellore is a city and the administrative headquarters of Vellore District in the South Indian state of Tamil Nadu. Located on the banks of the Palar River in the north-eastern part of Tamil Nadu, the city has been ruled, at different times, by the Pallavas, Medieval Cholas, Later Cholas, Vijayanagar (...)</summary>
        <feature/>
        <countryCode>IN</countryCode>
        <elevation>218</elevation>
        <lat>12.9202</lat>
        <lng>79.1333</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Vellore</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/155000/thumb-154812-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Kozhikode</title>
        <summary>Kozhikode , also known as Calicut, is a city in Kerala, India and the headquarters of the Kozhikode district. The Kozhikode metropolitan area is the second largest urban agglomeration in Kerala with a population of 2 million as of 2011 (...)</summary>
        <feature/>
        <countryCode>IN</countryCode>
        <elevation>15</elevation>
        <lat>11.2554</lat>
        <lng>75.7812</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Kozhikode</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/113000/thumb-112467-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>M. Chinnaswamy Stadium</title>
        <summary>The M. Chinnaswamy Stadium, located in Bengaluru, Karnataka, is a stadium owned by the Government of Karnataka. Flanked by the picturesque Cubbon Park, Queen's Road, Cubbon and uptown MG Road, this four decade old stadium is situated in the heart of the city of Bengaluru (...)</summary>
        <feature>landmark</feature>
        <countryCode>IN</countryCode>
        <elevation>936</elevation>
        <lat>12.9788</lat>
        <lng>77.5996</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/M._Chinnaswamy_Stadium</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/125000/thumb-124296-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Pondicherry</title>
        <summary>Pondicherry (or), officially known as Puducherry, is the capital and the most populous city of the Indian Union Territory of Puducherry, with a population of 657,209 and an area of 492 sq km. The city is situated in Puducherry district of the union territory, and is surrounded by the state of Tamil (...)</summary>
        <feature/>
        <countryCode>IN</countryCode>
        <elevation>12</elevation>
        <lat>11.931</lat>
        <lng>79.7852</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Pondicherry</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/132000/thumb-131071-100.png</thumbnailImg>
        <rank>100</rank>
    </entry>
    <entry>
        <lang>en</lang>
        <title>Chennai International Airport</title>
        <summary>Chennai International Airport is an international airport serving the city of Chennai, Tamil Nadu, India and its metropolitan area. It is located in Meenambakkam &amp; Tirusulam, 21 km (13 mi) from the city centre. It is the 4th busiest airport in India behind Delhi, Mumbai and Bengaluru and the 52nd (...)</summary>
        <feature>airport</feature>
        <countryCode>IN</countryCode>
        <elevation>11</elevation>
        <lat>12.9944</lat>
        <lng>80.1805</lng>
        <wikipediaUrl>https://en.wikipedia.org/wiki/Chennai_International_Airport</wikipediaUrl>
        <thumbnailImg>http://www.geonames.org/img/wikipedia/9000/thumb-8444-100.jpg</thumbnailImg>
        <rank>100</rank>
    </entry>
</geonames>

Regards,
burque505

3 Likes