The XML_PARSE function takes an XML (eXtensible Markup Language) file or string and converts it to an IDL variable.

This routine is written in the IDL language. Its source code can be found in the file xml_parse.pro in the lib subdirectory of the IDL distribution.

Tip: The result of XML_PARSE is an ORDEREDHASH. If you print out the result using Implied Print, then the output will automatically be printed in JSON format.

Examples


Assume xmlFile is a string containing the path to an XML file that contains the following:

<top topattr='1'>
  <tag1>12</tag1>
  <tag2 tagattr='tagtext'>
    valtext
  </tag2>
  <tag3>aa</tag3>
  <tag3>bb</tag3>
</top>

Parse XML into an ORDEREDHASH

result = XML_PARSE(xmlFile)
HELP, result
result

IDL prints:

RESULT          ORDEREDHASH  <ID=172  NELEMENTS=1>
{   
  "top": {
            "%topattr": "1",
       "tag1": "12",
                    "tag2": {
                        "%tagattr": "tagtext",
                        "#text": "valtext"
                },
                "tag3": [
                        "aa",
                        "bb"
                ]
        }
}

Get the value of a specific attribute

PRINT, result['top', '%topattr']

IDL prints:

1

Get the value of a specific tag

PRINT, result['top', 'tag1']
PRINT, result['top', 'tag2', '#text']

IDL prints:

12
valtext

Get the value of a specific tag inside a list

PRINT, result['top', 'tag3', 1]

IDL prints:

bb

Note: Since IDL is zero-based, the 1 refers to the second item in the list.

Syntax


Result = XML_PARSE(Input, IGNORE=string)

Return Value


The result is an ORDEREDHASH containing the ordered key-value pairs contained within the XML.

When converting XML values to IDL variables, the following rules are used:

  • A unique XML object becomes an IDL ORDEREDHASH variable.
  • An XML tag that is not unique at a given level becomes an IDL LIST variable.
  • All tag values become IDL strings.
  • If a tag has no attributes, the value is returned as a hash. The hash key is the same as the name of the XML tag.
  • If a tag has attributes or children, the value returned in the hash is another ORDEREDHASH.
  • Attributes are returned as key/value pairs in a hash where the name of the attribute is pre-appended with the character '%'.
  • The value of a tag that also contains attributes is returned as a key/value pair where the name of the key is '#text'.

Arguments


Input

Input must be the name of an XML file or a scalar string containing the XML to be parsed.

Keywords


IGNORE

Set this keyword to a string or an array of strings containing the names of the XML tags that should be ignored and not parsed into the resulting IDL variable. The strings in IGNORE are case insensitive.

Version History


8.6.1

Introduced

See Also


ORDEREDHASH, LIST