Name

loadXML()

Description

Reads the contents of a file or URL and creates an XML object with its values. If a file is specified, it must be located in the sketch's "data" folder. The filename parameter can also be a URL to a file found online.

All files loaded and saved by the Processing API use UTF-8 encoding. If you need to load an XML file that's not in UTF-8 format, see the developer's reference for the XML object.

Examples

  • // The following short XML file called "mammals.xml" is parsed 
    // in the code below. It must be in the project's "data" folder.
    //
    // <?xml version="1.0"?>
    // <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>
    
    XML xml;
    
    void setup() {
      xml = loadXML("mammals.xml");
      XML[] children = xml.getChildren("animal");
    
      for (int i = 0; i < children.length; i++) {
        int id = children[i].getInt("id");
        String coloring = children[i].getString("species");
        String name = children[i].getContent();
        println(id + ", " + coloring + ", " + name);
      }
    }
    
    // Sketch prints:
    // 0, Capra hircus, Goat
    // 1, Panthera pardus, Leopard
    // 2, Equus zebra, Zebra
    

Syntax

  • loadXML(filename)

Parameters

  • filename(String)name of a file in the data folder or a URL.

Return

  • XML