Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

Parse, query, modify, alter, transform or store XML with Robocorp and Robot Framework

Get the code and run this example in your favorite editor on our Portal!

Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. - Wikipedia - XML

Robocorp has full support for working with XML thanks to Robot Framework and other open-source projects. You can parse, query, modify, alter, transform or store XML with complete XPath, XQuery, and XSLT support.

The XML library is a great starting point when working with XML. Robocorp's native Python support enables the use of Python libraries, should you need additional XML capabilities.

Here are some things you can do with Robocorp's XML support:

  • Parse an XML file.
  • Get elements from XML by XPath.
  • Get elements from XML by element value.
  • Get elements from XML by attribute value.
  • Get element children from XML by XPath.
  • Access XML element object attributes.
  • Get attribute values from XML by XPath.
  • Get named attribute value from XML by XPath.
  • Save XML to a file.
  • Remove elements from XML by XPath.
  • Remove element attributes from XML by XPath.
  • Set XML element attribute value.
  • Add attribute to XML element by XPath.
  • Transform XML using XSLT.

Example XML document

robots.xml:

<?xml version="1.0" encoding="UTF-8"?> <robots> <robot id="MK-I"> <price currency="USD">1000</price> <nick-name>Spider</nick-name> <components> <legs>8</legs> <eyes>20</eyes> </components> <customization-options> <colors> <color>gold</color> <color>silver</color> <color>bronze</color> </colors> <power-sources> <power-source type="solar"/> <power-source type="lithium-ion-battery"/> <power type="coal"/> </power-sources> </customization-options> <dimensions> <dimension type="weight" unit="kg" value="160"/> <dimension type="height" unit="cm" value="240"/> <dimension type="width" unit="cm" value="300"/> <dimension type="length" unit="cm" value="150"/> </dimensions> </robot> <robot id="MK-II"> <price currency="USD">2000</price> <nick-name>Quad Arms</nick-name> <components> <arms>4</arms> <legs>2</legs> </components> <customization-options> <colors> <color>bronze</color> </colors> <power-sources> <power-source type="hamster"/> <power type="steam"/> </power-sources> </customization-options> <dimensions> <dimension type="weight" unit="kg" value="40"/> <dimension type="height" unit="cm" value="100"/> <dimension type="width" unit="cm" value="60"/> <dimension type="length" unit="cm" value="70"/> </dimensions> </robot> <robot id="MK-III"> <price currency="USD">50000</price> <nick-name>Eye Spy With Ten Little Eyes</nick-name> <components> <eyes>10</eyes> </components> <customization-options> <colors> <color>midnight</color> <color>blue</color> </colors> <power-sources> <power-source type="solar"/> </power-sources> </customization-options> <dimensions> <dimension type="weight" unit="kg" value="5"/> <dimension type="height" unit="cm" value="30"/> <dimension type="width" unit="cm" value="40"/> <dimension type="length" unit="cm" value="80"/> </dimensions> </robot> </robots>

Example XML robot using Robot Framework

*** Settings *** Documentation Robot Framework XML examples. Library XML use_lxml=True Library XmlTransformer.py *** Variables *** ${XML_FILE_PATH}= ${CURDIR}${/}robots.xml *** Tasks *** Parse an XML file ${xml}= Parse Xml ${XML_FILE_PATH} Log Element ${xml} Get elements from XML by XPath ${xml}= Parse test XML ${elements}= Get Elements ${xml} //robot FOR ${element} IN @{elements} Log Element ${element} END Get elements from XML by element value ${xml}= Parse test XML ${elements}= ... Get Elements ... ${xml} ... //robot[customization-options/colors[color='gold']] FOR ${element} IN @{elements} Log Element ${element} END Get elements from XML by attribute value ${xml}= Parse test XML ${elements}= ... Get Elements ... ${xml} ... //robot[dimensions/dimension[@type='weight' and @value>100]] FOR ${element} IN @{elements} Log Element ${element} END Get element children from XML by XPath ${xml}= Parse test XML ${elements}= Get Child Elements ${xml} //robot[1] FOR ${element} IN @{elements} Log Element ${element} END Access XML element object attributes ${xml}= Parse test XML # https://docs.python.org/3/library/xml.etree.elementtree.html#element-objects ${element}= Get Element ${xml} //robot[1] Log ${element.tag} # robot Log ${element.text} Log ${element.tail} Log ${element.attrib} # {'id': 'MK-I'} Get attribute values from XML by XPath ${xml}= Parse test XML ${attributes}= ... Get Element Attributes ... ${xml} ... //robot[1]/dimensions/dimension[1] Log ${attributes} # {'type': 'weight', 'unit': 'kg', 'value': '160'} Get named attribute value from XML by XPath ${xml}= Parse test XML ${attribute}= Get Element Attribute ${xml} id //robot[1] Log ${attribute} # MK-I Save XML to a file ${xml}= Parse test XML Save Xml ${xml} ${OUTPUT_DIR}${/}saved.xml Remove elements from XML by XPath ${xml}= Parse test XML ${result_xml}= Remove Elements ${xml} //customization-options Log Element ${result_xml} Remove element attributes from XML by XPath ${xml}= Parse test XML Remove Elements Attributes ${xml} //dimension Log Element ${xml} Set XML element attribute value ${xml}= Parse test XML ${element}= Get Element ${xml} //robot[1] Set Element Attribute ${element} id New-Value ${attribute}= Get Element Attribute ${element} id Log ${attribute} # New-Value Add attribute to XML element by XPath ${xml}= Parse test XML Set Element Attribute ${xml} my-attribute my-value //robot[1] ${attribute}= ... Get Element Attribute ... ${xml} ... my-attribute ... //robot[1] Log ${attribute} # my-value Transform XML using XSLT Transform Xml ... ${XML_FILE_PATH} ... ${CURDIR}${/}transform-to-humans.xsl ... ${OUTPUT_DIR}${/}humans.xml *** Keywords *** Parse test XML ${xml}= Parse Xml ${XML_FILE_PATH} RETURN ${xml}
Last edit: May 4, 2022