This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Class

XML

Name

format()

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");
  
  //Format without line breaks and no indentation
  String s = xml.format(-1);
  println(s);
  println("");  // Blank line
  
  //Format with line breaks and no indentation
  s = xml.format(0);
  println(s);
  
  //Format with line breaks and 5 spaces of indentation
  s = xml.format(5);
  println(s);
}

// Sketch prints:
//<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 version="1.0" encoding="UTF-8"?>
//<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 version="1.0" encoding="UTF-8"?>
//<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>
Description Takes an XML object and converts it to a String, formatting its content as specified with the indent parameter.

If indent is set to -1, then the String is returned with no line breaks, no indentation, and no XML declaration.

If indent is set to 0 or greater, then the String is returned with line breaks, and the specified number of spaces as indent values. Meaning, there will be no indentation if 0 is specified, or each indent will be replaced with the corresponding number of spaces: 1, 2, 3, and so on.
Syntax
.format(indent)
Parameters
indent int: -1 for a single line (and no declaration), >= 0 for indents and newlines
ReturnsString
RelatedtoString()
Updated on January 1, 2021 03:38:10am EST