The global XML object

The global XML Object

E4X-capable JavaScript engines put a new property on the global object. The XML object has several properties that allow you to customize parsing and serialization of E4X. XML elements will remember the settings of the XML object from the time of their creation.

Extending XML.prototype

XML.prototype and XMLList.prototype (XMLList.prototype is actually just XML.prototype) cannot be extended in the same conventional way as other constructors such as Object. You can only define methods in XML.prototype and not fields. To add a method to XML.prototype, define XML.prototype.function::methodName or XML.prototype.function::[methodNameString]. The following example defines the fooCount() method, which returns the amount of <foo> elements in the XML:

XML.prototype.function::fooCount = function fooCount() {
  return this..foo.length();
};
<foobar><foo/><foo/><foo/></foobar>.fooCount() // returns 3

ignoreComments

true by default. This property tells E4X to ignore comment nodes when serializing and filtering. This means that if ignoreComments is true, the list returned by .comments() will be empty. Observe:

var element = <foo>
                <!-- my comment -->
                <bar/>
              </foo>;
element.comments().length(); // returns 0
element.toXMLString(); // returns <foo><bar/></foo>
XML.ignoreComments = false;
element = <foo>
            <!-- my comment -->
            <bar/>
          </foo>;
element.comments().length(); // returns 1
element.toXMLString(); // returns <foo><!-- my comment --><bar/></foo>

ignoreProcessingInstructions

true by default. This property tells E4X to ignore processing instructions in XML when serializing and filtering. For example

var element = <foo>
                <?process x="true"?>
                <bar/>
                <?process x="false"?>
              </foo>;
element.toXMLString(); // returns <foo><bar/></foo>
XML.ignoreProcessingInstructions = false;
var element = <foo>
                <?process x="true"?>
                <bar/>
                <?process x="false"?>
              </foo>;
element.toXMLString(); // returns <foo><?process x="true"?><bar/><?process x="false"?></foo>

ignoreWhitespace

true by default. Ignores whitespace between nodes and leading and trailing whitespace in text nodes, which would otherwise be interpreted as text nodes or as part of those text nodes, respectively.

prettyPrinting

true by default. When true, toXMLString() includes newlines and indenting for the serialization of E4X objects.

prettyIndent

2 by default. The number of spaces to indent each level in the XML tree. Ignored if prettyPrinting is false.

Document Tags and Contributors

 Contributors to this page: Sheppy, Procoffeev, alysergeev, Brettz9, Sephr, Vor0nwe, Waldo, Jminta
 Last updated by: Sheppy,