Skip to main content

XML

xml_parse​

Parses XML content and returns matching elements as JSON objects.

Returns an array of matched element objects. Throws an exception if the XML is invalid or the file does not exist.

let elements = xml_parse(xml, selector, [options]);
let elements = xml_parse(filestore, filename, selector, [options]);

Parameters:

  • xml: String containing XML.
  • filestore: String containing name of file repository.
  • filename: String containing filename.
  • selector: String containing XPath-like path to elements that should be returned (for example /MOVIES/MOVIE).
  • options: Object containing options to configure the XML parsing behaviour:
    • from: Integer offset from which to start returning elements (optional, default: 0).
    • limit: Integer limit for the number of elements that are returned (optional, default: 0, which means the maximum limit is used).

Limits​

By default, the result of each xml_parse call is limited to a maximum of 10,000 elements. You can specify a lower limit by using the limit parameter (see example 3 below). If a higher limit is specified, the maximum of 10,000 is used. You can iterate over entire document by using the from parameter (see example 4 below).

The maximum size of a matched XML element is 1 megabyte of data. An exception is thrown if the element exceeds that size.

Examples​

The following examples use this simple XML document as either a string or the contents of a file:


<MOVIES>
<MOVIE>
<TITLE>The Shawshank Redemption</TITLE>
<YEAR>1994</YEAR>
</MOVIE>
<MOVIE>
<TITLE>The Godfather</TITLE>
<YEAR>1972</YEAR>
</MOVIE>
</MOVIES>

Example 1: parse xml string

xml_parse(xml, "/MOVIES/MOVIE");
// => [
// { TITLE: "The Shawshank Redemption", YEAR: 1994 },
// { TITLE: "The Godfather", YEAR: 1972 }
// ]

Example 2: parse xml file

let filestore = "imdb";
let filename = "best_movies.xml";
let elements = xml_parse(filestore, filename, "/MOVIES/MOVIE");
// => [
// { TITLE: "The Shawshank Redemption", YEAR: 1994 },
// { TITLE: "The Godfather", YEAR: 1972 }
// ]

Example 3: parse large xml files (> 10,000 elements)

let filestore = "imdb";
let filename = "best_movies.xml";

let options = { from: 0 };
while (true) {
let elements = xml_parse(filestore, filename, "/MOVIES/MOVIE", options);
if (elements.length == 0) {
break;
}

// do something with elements

from += elements.length;
}

Example 4: parse xml files in small batches

let filestore = "imdb";
let filename = "best_movies.xml";

let options = {
from: 0,
limit: 100,
};
while (true) {
let elements = xml_parse(filestore, filename, "/MOVIES/MOVIE", options);
if (elements.length == 0) {
break;
}

// do something with elements

from += elements.length;
}