XML.stringify()

The XML.stringify() API transforms XML DOM node objects into strings.

Syntax

var output = XML.stringify(object);
var output = XML.stringify(options,object);
outputThe string representation of the object after a successful transform.
objectThe Node or NodeList to transform.
optionsThe JSON object that contains specifications to control output.
The XML.stringify() API has two forms: With and without a JSON options object. This API supports only the following options.
omitXmlDeclaration
Indicates whether to omit the XML declaration in the output. The following code shows an example of an XML declaration.
<?XML version="1.0">
true
Omits the XML declaration in the output.
false
Retains the XML declaration in the output. This setting is the default value.
escaping
Defines minimum output-escaping that is often used when processing non-English characters. The following values are supported.
minimal
Outputs character-representation whenever possible.
maximum
Outputs XML numeric character entities for non-ASCII characters whenever possible. This setting is the default value.
The data provided for the object is a node or a node list.
  • A node includes all subclasses.
  • A node list is a collection of nodes.

The XML.stringify() API throws an exception when the object is not a Node or NodeList.

Example

  • Parse an XML document into data string according to options.
    var doc = XML.parse(
    '<books xmlns="http://abc.com/books">\n' +
    ' <book><title>JavaScript</title><price>22.99</price></book>\n' +
    ' <book><title>XSLT</title><price>35</price></book>\n' +
    '</books>');
    var option = {omitXmlDeclaration: false};
    var output = XML.stringify(option,doc);
    The output is as follows.
    <?xml version="1.0" encoding="UTF-8"?>
    <books xmlns="http://abc.com/books">
     <book><title>JavaScript</title><price>22.99</price></book>
     <book><title>XSLT</title><price>35</price></book>
    </books>
  • Parse a data string that contains non-English characters and retain these characters.
    var output = XML.stringify({omitXmlDeclaration: true, escaping: "minimal"}, XML.parse('mañana'));
    session.output.write(output);
    • With the escaping options, the output is mañana.
    • Without the escaping options, the output is ma&#241;ana.