XML.stringify()

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

var output = XML.stringify(object);

var output = XML.stringify(options,object);

Parameters

output
The string representation of the object after a successful transform.
object
The Node or NodeList to transform.
options
The JSON object that contains specifications to control output.

Guidelines

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 to process characters in languages other than English. 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 that is 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 raises 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 characters in languages other than English and retain these characters.
    var output = XML.stringify({omitXmlDeclaration: true, escaping: "minimal"}, XML.parse('<a>mañana</a>'));
    session.output.write(output);
    • With the escaping options, the output is <a>mañana</a>.
    • Without the escaping options, the output is <a>ma&#241;ana</a>.