Static p5_sys::global::loadXML[][src]

pub static loadXML: LoadXmlInternalType
Expand description

Reads the contents of a file and creates an XML object with its values. If the name of the file is used as the parameter, as in the above example, the file must be located in the sketch directory/folder.

Alternatively, the file maybe be loaded from anywhere on the local computer using an absolute path (something that starts with / on Unix and Linux, or a drive letter on Windows), or the filename parameter can be a URL for a file found on a network.

This method is asynchronous, meaning it may not finish before the next line in your sketch is executed. Calling loadXML() inside preload() guarantees to complete the operation before setup() and draw() are called.

Outside of preload(), you may supply a callback function to handle the object.

This method is suitable for fetching files up to size of 64MB.

Examples

// The following short XML file called "mammals.xml" is parsed
// in the code below.
//
// 
// <mammals>
//   <animal id="0" species="Capra hircus">Goat</animal>
//   <animal id="1" species="Panthera pardus">Leopard</animal>
//   <animal id="2" species="Equus zebra">Zebra</animal>
// </mammals>

let xml;

function preload() {
  xml = loadXML('assets/mammals.xml');
}

function setup() {
  let children = xml.getChildren('animal');

  for (let i = 0; i < children.length; i++) {
    let id = children[i].getNum('id');
    let coloring = children[i].getString('species');
    let name = children[i].getContent();
    print(id + ', ' + coloring + ', ' + name);
  }
}

// Sketch prints:
// 0, Capra hircus, Goat
// 1, Panthera pardus, Leopard
// 2, Equus zebra, Zebra

Parameters

filename name of the file or URL to load

callback? function to be executed after loadXML() completes, XML object is passed in as first argument

errorCallback? function to be executed if there is an error, response is passed in as first argument