XML_PARSE

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:

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