Skip to main content
OutSystems

How to parse XML files with XPath expressions

Question

How to use XPath to extract values from XML to database records?

Answer

The OutSystems Platform has some bundled resources to parse XML files, such as the XML Extension, that includes support for XPath expressions and data manipulation.

Below are the followed steps to create some books information on the database based on a XML file.

  1. Load the XML file

    LoadXML.png

    The first thing you will need to do it's to load you Xml file by using the XmlDocument_Load action available on the XML extension. The Xml input parameter needs to be a text so you need to use the BinaryDataToText action available on the BinaryData extension, to convert the binary of the Xml file into text.​​
     
  2. Run the XPath filter over the XML



    Based on our Xml definition we are currently filtering our Xml file to only get the nodes that belong to the WEB category. For that we use the XmlDocument_SelectNodes action available on the XML extension.
     
  3. Iterate over resulting nodes



    Using the XmlNodeList_Count action we will get the number of nodes that currently exist on our filtered Xml so, based on that number, we can iterate over those nodes to get the information for each one of the nodes.
     
  4. Get each node value



    Using the XmlNodeList_Item action we will get the information of the current node that we are iterating, and then, for selecting a single Xml attribute of that node we use the XmlElement_SelectSingleNode by setting the XPathString input parameter to the attribute that we want to get, and by last, we use the XmlElement_GetInnerText action to extract the data from that Xml attribute.


     
  5. Assign values to Entity Record variable



    By last, we just need to assign the values that we have obtained from our node attributes to our entity record variable, and then, by using the supported mechanisms, create the data in the database.

    On the above example the Current it's an Integer local variable that we are using to control the iteration over the nodes, so if you follow the same approach please don't forget to increment that variable at the end of each cycle, otherwise you will end up with duplicated data.

Attached is a simple eSpace (XPathExample.oml) with the complete example so you can see the full implementation details and apply the same pattern to your specific use case.

https://success.outsystems.com/@api/deki/files/1232/XPathExample.oml