Main Page

XML code

var oXmlSettings = XML.settings(); //save these settings
XML.prettyIndent = 4;
XML.ignoreWhitespace = true;
//do something here
XML.setSettings(oXmlSettings); //return to the original settings
Here, the settings are initially saved to
oXmlSettings
before being changed. The settings are then reset
using the
setSettings()
method. It is also possible to retrieve the default settings of the parser by
using the
XML.defaultSettings()
method:
XML.setSettings(XML.defaultSettings());
As for XML object instances, you have a large number of methods available to manipulate XML data.
Some of these methods are inspired by the DOM; others are not, but all are useful.
The first group of methods deals with namespaces and
Namespace
objects. Most of these are pretty
straightforward:
addNamespace()
adds a given namespace to the element and
removeNamespace()
removes a given namespace from the element.
var oWroxNS = new Namespace(“wrox”, “http://www.wrox.com/”);
var oXml = <message>Hello World!</message>;
oXml.addNamespace(oWroxNS);
//do something else
oXml.removeNamespace(oWroxNS);
It’s also possible to retrieve arrays of namespaces by using the
inScopeNamespaces()
and
namespaceDeclarations()
methods. Each of these return an array of
Namespace
objects, with
inScopeNamespaces()
returning only those namespaces from the current element down and
namespaceDeclarations()
returning all namespaces represented from the root element down.
To deal with individual namespaces, you can use
namespace()
to retrieve a namespace and
setNamespace()
, as you might assume, to set a namespace for the current element.
var oXml = <wrox:root xmlns:wrox=”http://www.wrox.com/”>
<wrox:message>Hello World!</wrox:message>
</wrox:root>;
var oWroxNS = oXml.namespace();
var oNewNS = new Namespace(“ncz”, “http://www.nczonline.net/”);
oXml.setNamespace(oNewNS);
Attributes are very easy to access in E4X by using the
attribute()
and
attributes()
method. The
attribute()
method returns the value of the attribute. The
attributes()
method returns an
XMLList
(which is covered in the next section) containing all attributes for the given element.
var oXml = <value type=”string”>Hello World!</value>
var sType = oXml.attribute(“type”); //set to “string”
var oAtts = oXml.attributes();
Children of an XML element can be accessed using either the
child()
method, which returns a single
XML
object indicated by either the child’s index or name, or the
children()
method, which returns an
XMLList
of all children. Additionally the
childIndex()
method can be used to determine the location
of an element among its siblings. For example:
610
Chapter 20
23_579088 ch20.qxd 3/28/05 11:44 AM Page 610


JavaScript EditorFree JavaScript Editor     Ajax Editor


©