Parsing complex XML files
Question

Hi,

I have to parse complex XML files. I've been playing around for a while now but I'm stuck. The default XML tools in OutSystems don't seem to get me anywhere. Same goes for the XmlRecords component in the forge. Not sure, but I think it's because the XML contains nested elements.

The XML contains a list of products with product information. It looks something like this:

  • <products>
    • <product>
      • <division>X</division>
      • <details>
        • <id>A01</id>
        • <status>Active</status>
        • <kept_in_stock>1</kept_in_stock>
      • </details>
      • <product_desciptions>
        • <product_desciption>
          • <desciption><![CDATA[Hob 88 cm]]></desciption>
          • <type>Name</type>
        • </product_desciption>
        • <product_desciption>
          • <desciption><![CDATA[Very nice hob 88 cm]]></desciption>
          • <type>Short description</type>
        • </product_desciption>
      • <product_desciptions>
      • <prices>
        • <price>1.00</price>
      • </prices>
      • <features>
        • <feature>
          • <id>x1</id>
          • <feature_value>
            • <value><![CDATA[Yes]]></value>
            • <language>EN</language>
          • </feature_value>
        • </feature>
        • <feature>
          • <id>x2</id>
          • <feature_value>
            • <value><![CDATA[15 mm]]></value>
            • <language>EN</language>
          • </feature_value>
        • </feature>
      • </features>
    • </product>
    • ...
  • <products>

I'm able to read elements like <division> but that's it. Because it's a large XML file I would like to split the content and create a record per product in an entity so I can process them later and, if an error occurs, retry without having to process the complete XML again.

Can anyone shed some light on how to parse the XML? How can I get this XML into a record structure?

Thanks in advance.

Cheers,
Ramon

Solution

Hello @Ramon Pigmans 

The key to using the XML Records forge component, is to build the appropriate structure(s) for your xml. In the example you have provided, you will need the following structure:

I have attached the oml that uses the above structure.

Just click the Parse button to execute the demo. As an example I chose to parse the features and display it on the screen:

Best regards,

AJ

ParseXMLDemo.oml

Hi Ramon,

You can convert xml to record list using below forge component

https://www.outsystems.com/forge/component-overview/153/xml-records

Thanks, Aadhavan S

Thanks for your answer @Aadhavan Shanmugam but if I'm not mistaken, XmlRecords does not support nested elements. Correct me if I'm wrong.

mvp_badge
MVP

As the component's description states, it "allows you to convert an hierarchical structure", and it has a picture showing nexted structures. So I'd be surprised if what you think is true.

Yes. As Kilian Hekhuis mentioned, the component description itself mentioned the extension can able to convert a hierarchical structure. 

Thanks, Aadhavan S


Well, the description of the forge component states "Records composed of multiple structures are not supported". Also in the demo, the structure used has only one level. Guess I was thrown off by that and apparently I misinterpreted this.

mvp_badge
MVP

Yeah, that's perhaps not very clear. What they mean is Records that have multiple Structures at the same level, like the output of Aggragates. For example:

Solution

Hello @Ramon Pigmans 

The key to using the XML Records forge component, is to build the appropriate structure(s) for your xml. In the example you have provided, you will need the following structure:

I have attached the oml that uses the above structure.

Just click the Parse button to execute the demo. As an example I chose to parse the features and display it on the screen:

Best regards,

AJ

ParseXMLDemo.oml

Thanks a million @AJ. I've tried your solution and it works like a charm. I'll take this as the base for my solution and will develop it further. Thank you for your answer and taking the time to create a working example! This helps me a lot.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.