Name

parseXML()

Description

Takes a String, parses its contents, and returns an XML object. If the String does not contain XML data or cannot be parsed, a null value is returned.

parseXML() is most useful when pulling data dynamically, such as from third-party APIs. Normally, API results would be saved to a String, and then can be converted to a structured XML object using parseXML(). Be sure to check if null is returned before performing operations on the new XML object, in case the String content could not be parsed.

If your data already exists as an XML file in the data folder, it is simpler to use loadXML().

Examples

  • String data = "<mammals><animal>Goat</animal></mammals>";
    
    void setup() {
      XML xml = parseXML(data);
      if (xml == null) {
        println("XML could not be parsed.");
      } else {
        XML firstChild = xml.getChild("animal");
        println(firstChild.getContent());
      }
    }
    
    // Sketch prints:
    // Goat
    

Syntax

  • parseXML(xmlString)
  • parseXML(xmlString, options)

Parameters

  • xmlString(String)the content to be parsed as XML

Return

  • XML