| Package | Top Level |
| Class | public final dynamic class XML |
| Inheritance | XML Object |
Use the toXMLString() method to return a string representation of the XML object
regardless of whether the XML object has simple content or complex content.
Note: The XML class (and related classes) from ActionScript 2.0 has been renamed XMLDocument and moved into the flash.xml package. It is included in ActionScript 3.0 for backward compatibility.
View the examples. See also| Namespace, QName, XMLList, XML.toXMLString(), ECMAScript for XML (E4X) specification (ECMA-357 edition 2) |
| Property | Defined by | ||
|---|---|---|---|
![]() | constructor : Object
A reference to the class object or constructor function for a given object instance.
| Object | |
| ignoreComments : Boolean
[static]
Determines whether XML comments are ignored
when XML objects parse the source XML data.
| XML | ||
| ignoreProcessingInstructions : Boolean
[static]
Determines whether XML
processing instructions are ignored when XML objects parse the source XML data.
| XML | ||
| ignoreWhitespace : Boolean
[static]
Determines whether white space characters
at the beginning and end of text nodes are ignored during parsing.
| XML | ||
| prettyIndent : int
[static]
Determines the amount of indentation applied by
the
toString() and toXMLString() methods when
the XML.prettyPrinting property is set to true. | XML | ||
| prettyPrinting : Boolean
[static]
Determines whether the
toString()
and toXMLString() methods normalize white space characters between some tags. | XML | ||
![]() | prototype : Object
[static]
A reference to the prototype object of a class or function object.
| Object | |
| Function | Defined by | ||
|---|---|---|---|
|
Creates a new XML object.
| XML | ||
|
Adds a namespace to the set of in-scope namespaces for the XML object.
| XML | ||
|
Appends the given child to the end of the XML object's properties.
| XML | ||
|
Returns the XML value of the attribute that has the name matching the
attributeName
parameter. | XML | ||
|
Returns a list of attribute values for the given XML object.
| XML | ||
|
Lists the children of an XML object.
| XML | ||
|
Identifies the zero-indexed position of this XML object within the context of its parent.
| XML | ||
|
Lists the children of the XML object in the sequence in which they appear.
| XML | ||
|
Lists the properties of the XML object that contain XML comments.
| XML | ||
|
Compares the XML object against the given
value parameter. | XML | ||
|
Returns a copy of the given XML object.
| XML | ||
|
[static]
Returns an object with the following properties set to the default values:
ignoreComments,
ignoreProcessingInstructions, ignoreWhitespace, prettyIndent, and
prettyPrinting. | XML | ||
|
Returns all descendants (children, grandchildren, great-grandchildren, and so on) of the
XML object that have the given
name parameter. | XML | ||
|
Lists the elements of an XML object.
| XML | ||
|
Checks to see if the XML object contains complex content.
| XML | ||
|
Checks to see if the object has the property specified by the
p parameter. | XML | ||
|
Checks to see if the XML object contains simple content.
| XML | ||
|
Lists the namespaces for the XML object, based on the object's parent.
| XML | ||
|
Inserts the given
child2 parameter after the child1 parameter in this XML object and returns the
resulting object. | XML | ||
|
Inserts the given
child2 parameter before the child1 parameter
in this XML object and returns the resulting object. | XML | ||
![]() |
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter.
| Object | |
|
For XML objects, this method always returns the integer
1. | XML | ||
|
Gives the local name portion of the qualified name of the XML object.
| XML | ||
|
Gives the qualified name for the XML object.
| XML | ||
|
If no parameter is provided, gives the namespace associated with the qualified name of
this XML object.
| XML | ||
|
Lists namespace declarations associated with the XML object in the context of its parent.
| XML | ||
|
Specifies the type of node: text, comment, processing-instruction,
attribute, or element.
| XML | ||
|
For the XML object and all descendant XML objects, merges adjacent text nodes and
eliminates empty text nodes.
| XML | ||
|
Returns the parent of the XML object.
| XML | ||
|
Inserts a copy of the provided
child object into the XML element before any existing XML
properties for that element. | XML | ||
|
If a
name parameter is provided, lists all the children of the XML object
that contain processing instructions with that name. | XML | ||
|
Checks whether the property
p is in the set of properties that can be iterated in a
for..in statement applied to the XML object. | XML | ||
|
Removes the given namespace for this object and all descendants.
| XML | ||
|
Replaces the properties specified by the
propertyName parameter
with the given value parameter. | XML | ||
|
Replaces the child properties of the XML object with the specified set of XML properties,
provided in the
value parameter. | XML | ||
|
Changes the local name of the XML object to the given
name parameter. | XML | ||
|
Sets the name of the XML object to the given qualified name or attribute name.
| XML | ||
|
Sets the namespace associated with the XML object.
| XML | ||
![]() |
Sets the availability of a dynamic property for loop operations.
| Object | |
|
[static]
Sets values for the following XML properties:
ignoreComments,
ignoreProcessingInstructions, ignoreWhitespace,
prettyIndent, and prettyPrinting. | XML | ||
|
[static]
Retrieves the following properties:
ignoreComments,
ignoreProcessingInstructions, ignoreWhitespace,
prettyIndent, and prettyPrinting. | XML | ||
|
Returns an XMLList object of all XML properties of the XML object that represent XML text nodes.
| XML | ||
|
Returns a string representation of the XML object.
| XML | ||
|
Returns a string representation of the XML object.
| XML | ||
|
Returns the XML object.
| XML | ||
ignoreComments:Boolean [read-write]true). To include XML comments, set this property to false.
The ignoreComments property is used only during the XML parsing, not during
the call to any method such as myXMLObject.child(*).toXMLString().
If the source XML includes comment nodes, they are kept or discarded during the XML parsing.
public static function get ignoreComments():Boolean
public function set ignoreComments(value:Boolean):void
XML.ignoreComments
to false and to true:
XML.ignoreComments = false;
var xml1:XML =
<foo>
<!-- comment -->
</foo>;
trace(xml1.toXMLString()); // <foo><!-- comment --></foo>
XML.ignoreComments = true;
var xml2:XML =
<foo>
<!-- example -->
</foo>;
trace(xml2.toXMLString()); // <foo/>
| XML.child(), XML.toXMLString() |
ignoreProcessingInstructions:Boolean [read-write]true). To include XML
processing instructions, set this property to false. The
ignoreProcessingInstructions property is used only during the XML parsing,
not during the call to any method such as myXMLObject.child(*).toXMLString().
If the source XML includes processing instructions nodes, they are kept or discarded during
the XML parsing.
public static function get ignoreProcessingInstructions():Boolean
public function set ignoreProcessingInstructions(value:Boolean):void
XML.ignoreProcessingInstructions
to false and to true:
XML.ignoreProcessingInstructions = false;
var xml1:XML =
<foo>
<?exampleInstruction ?>
</foo>;
trace(xml1.toXMLString());
XML.ignoreProcessingInstructions = true;
var xml2:XML =
<foo>
<?exampleInstruction ?>
</foo>;
trace(xml2.toXMLString());
| XML.child(), XML.toXMLString() |
ignoreWhitespace:Boolean [read-write]true). If a text node is 100% white space and the
ignoreWhitespace property is set to true, then the node is not created.
To show white space in a text node, set the ignoreWhitespace property to
false.
public static function get ignoreWhitespace():Boolean
public function set ignoreWhitespace(value:Boolean):void
XML.ignoreWhitespace
to false and to true:
XML.ignoreWhitespace = false; var xml1:XML = <foo> </foo>; trace(xml1.children().length()); // 1 XML.ignoreWhitespace = true; var xml2:XML = <foo> </foo>; trace(xml2.children().length()); // 0
prettyIndent:int [read-write]toString() and toXMLString() methods when
the XML.prettyPrinting property is set to true.
Indentation is applied with the space character, not the tab character.
The default value is 2.
public static function get prettyIndent():int
public function set prettyIndent(value:int):void
XML.prettyIndent
static property:
var xml:XML = <foo><bar/></foo>; XML.prettyIndent = 0; trace(xml.toXMLString()); XML.prettyIndent = 1; trace(xml.toXMLString()); XML.prettyIndent = 2; trace(xml.toXMLString());
| prettyPrinting, toString(), toXMLString() |
prettyPrinting:Boolean [read-write]toString()
and toXMLString() methods normalize white space characters between some tags.
The default value is true.
public static function get prettyPrinting():Boolean
public function set prettyPrinting(value:Boolean):void
XML.prettyPrinting
static property:
var xml:XML = <foo><bar/></foo>; XML.prettyPrinting = false; trace(xml.toXMLString()); XML.prettyPrinting = true; trace(xml.toXMLString());
| prettyIndent, toString(), toXMLString() |
public function XML(value:Object)
Use the toXMLString() method to return a string representation of the XML object
regardless of whether the XML object has simple content or complex content.
value:Object — Any object that can be converted to XML with the top-level
XML() function.
|
| top-level XML() function, XML.toXMLString() |
public function addNamespace(ns:Object):XML
undefined. If the input parameter
is a Namespace object, it's used directly. If it's a QName object, the input parameter's
URI is used to create a new namespace; otherwise, it's converted to a String and a namespace is created from
the String.
Parameters
ns:Object — The namespace to add to the XML object.
|
XML —
The new XML object, with the namespace added.
|
var xml1:XML = <ns:foo xmlns:ns="www.example.com/ns" />; var nsNamespace:Namespace = xml1.namespace(); var xml2:XML = <bar />; xml2.addNamespace(nsNamespace); trace(xml2.toXMLString()); // <bar xmlns:ns="www.example.com/ns"/>
public function appendChild(child:Object):XML
appendChild() method takes an XML object, an XMLList object, or
any other data type that is then converted to a String.
Parameters
child:Object — The XML object to append.
|
XML —
The resulting XML object.
|
var xml:XML =
<body>
<p>hello</p>
</body>;
xml.appendChild(<p>world</p>);
trace(xml.p[0].toXMLString()); //hello
trace(xml.p[1].toXMLString()); //world
public function attribute(attributeName:*):XMLList
attributeName
parameter. Attributes are found within XML elements.
In the following example, the element has an attribute named "gender"
with the value "boy": <first gender="boy">John</first>.
The attributeName parameter can be any data type; however,
String is the most common data type to use. When passing any object other than a QName object,
the attributeName parameter uses the toString() method
to convert the parameter to a string.
If you need a qualified name reference, you can pass in a QName object. A QName object
defines a namespace and the local name, which you can use to define the qualified name of an
attribute. Therefore calling attribute(qname) is not the same as calling
attribute(qname.toString()).
attributeName:* — The name of the attribute.
|
XMLList —
An XMLList object or an empty XMLList object. Returns an empty XMLList object
when an attribute value has not been defined.
|
attribute() method. The
localname property is attr and the namespace property
is ns.
var xml:XML = <ns:node xmlns:ns = "http://uri" ns:attr = '7' />
var qn:QName = new QName("http://uri", "attr");
trace (xml.attribute(qn)); // 7
attribute() method instead of the attribute identifier (@)
operator, as in the following example:
var xml:XML = <example class="first" />
trace(xml.attribute("class"));
| XML.attributes(), QName, Namespace, XML.elements(), attribute identifier (@) operator |
public function attributes():XMLList
name()
method with the attributes() method to return the name of an attribute.
Use @* to return the names of all attributes.
Returns
XMLList —
The list of attribute values.
|
var xml:XML=<example id='123' color='blue'/> trace(xml.attributes()[1].name()); //color
var xml:XML =<example id='123' color='blue'/>
var xml2:XMLList = x.@*;
trace (xml2 is XMLList); // true
trace (xml2.length()); // 2
for (var i:int = 0; i < xml2.length(); i++)
{
trace (typeof (xml2[i])); // xml
trace (xml2[i].nodeKind()); // attribute
trace (xml2[i].name()); // id and color
}
| XML.attribute(), XML.name(), @ operator |
public function child(propertyName:Object):XMLList
Use the propertyName parameter to list the
contents of a specific XML child. For example, to return the contents of a child named
<first>, use child.name("first"). You can generate the same result
by using the child's index number. The index number identifies the child's position in the
list of other XML children. For example, name.child(0) returns the first child
in a list.
Use an asterisk (*) to output all the children in an XML document.
For example, doc.child("*").
Use the length() method with the asterisk (*) parameter of the
child() method to output the total number of children. For example,
numChildren = doc.child("*").length().
propertyName:Object — The element name or integer of the XML child.
|
XMLList —
An XMLList object of child nodes that match the input parameter.
|
child() method to identify child
elements with a specified name:
var xml:XML =
<foo>
<bar>text1</bar>
<bar>text2</bar>
</foo>;
trace(xml.child("bar").length()); // 2
trace(xml.child("bar")[0].toXMLString()); // <bar>text1</bar>
trace(xml.child("bar")[1].toXMLString()); // <bar>text2</bar>
| XML.elements(), XMLList class, XML.length() |
public function childIndex():int
int —
The position of the object. Returns -1 as well as positive integers.
|
childIndex() method:
var xml:XML =
<foo>
<bar />
text
<bob />
</foo>;
trace(xml.bar.childIndex()); // 0
trace(xml.bob.childIndex()); // 2
public function children():XMLList
XMLList —
An XMLList object of the XML object's children.
|
children() method:
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
var xml:XML =
<foo id="22">
<bar>44</bar>
text
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml.children().length()); // 4
trace(xml.children()[0].toXMLString()); // <bar>44</bar>
trace(xml.children()[1].toXMLString()); // text
trace(xml.children()[2].toXMLString()); // <!-- comment -->
trace(xml.children()[3].toXMLString()); // <?instruction ?>
public function comments():XMLList
XMLList —
An XMLList object of the properties that contain comments.
|
comments() method:
XML.ignoreComments = false;
var xml:XML =
<foo>
<!-- example -->
<!-- example2 -->
</foo>;
trace(xml.comments().length()); // 2
trace(xml.comments()[1].toXMLString()); // <!-- example2 -->
public function contains(value:XML):Boolean
value parameter.
Parameters
value:XML — A value to compare against the current XML object.
|
Boolean —
If the XML object matches the value parameter, then true; otherwise false.
|
contains() method:
var xml:XML =
<order>
<item>Rice</item>
<item>Kung Pao Shrimp</item>
</order>;
trace(xml.p[0].contains(<p>Rice</p>)); // true
trace(xml.p[1].contains(<p>Kung Pao Shrimp</p>)); // true
trace(xml.p[1].contains(<p>MSG</p>)); // false
public function copy():XML
null if you attempt to call the
parent() method.
Returns
XML —
The copy of the object.
|
copy() method creates a new instance of an XML object.
When you modify the copy, the original remains unchanged:
var xml1:XML = <foo />; var xml2:XML = xml1.copy(); xml2.appendChild(<bar />); trace(xml1.bar.length()); // 0 trace(xml2.bar.length()); // 1
public static function defaultSettings():Object
ignoreComments,
ignoreProcessingInstructions, ignoreWhitespace, prettyIndent, and
prettyPrinting. The default values are as follows:
ignoreComments = trueignoreProcessingInstructions = trueignoreWhitespace = trueprettyIndent = 2prettyPrinting = trueNote: You do not apply this method to an instance of the XML class; you apply it to
XML, as in the following code: var df:Object = XML.defaultSettings().
Object —
An object with properties set to the default settings.
|
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
var customSettings:Object = XML.settings();
var xml1:XML =
<foo>
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml1.toXMLString());
// <foo>
// <!-- comment -->
// <?instruction ?>
// </foo>
XML.setSettings(XML.defaultSettings());
var xml2:XML =
<foo>
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml2.toXMLString());
| XML.ignoreComments, XML.ignoreProcessingInstructions, XML.ignoreWhitespace, XML.prettyIndent, XML.prettyPrinting, XML.setSettings(), XML.settings() |
public function descendants(name:Object = *):XMLList
name parameter. The name parameter
is optional. The name parameter can be a QName object, a String data type
or any other data type that is then converted to a String data type.
To return all descendants, use the "*" parameter. If no parameter is passed, the string "*" is passed and returns all descendants of the XML object.
Parametersname:Object (default = *) — The name of the element to match.
|
XMLList —
An XMLList object of matching descendants. If there are no descendants, returns an
empty XMLList object.
|
descendants() method instead of the descendant (..) operator, as in the
following example:
var xml:XML =
<enrollees>
<student id="239">
<class name="Algebra" />
<class name="Spanish 2"/>
</student>
<student id="206">
<class name="Trigonometry" />
<class name="Spanish 2" />
</student>
</enrollees>
trace(xml.descendants("class"));
descendants() method returns an XMLList object
that contains all descendant objects, including children, grandchildren, and so on:
XML.ignoreComments = false;
var xml:XML =
<body>
<!-- comment -->
text1
<a>
<b>text2</b>
</a>
</body>;
trace(xml.descendants("*").length()); // 5
trace(xml.descendants("*")[0]); // // <!-- comment -->
trace(xml.descendants("*")[1].toXMLString()); // text1
trace(xml.descendants("a").toXMLString()); // <a><b>text2</b></a>
trace(xml.descendants("b").toXMLString()); // <b>text2</b>
| descendant accessor (..) operator |
public function elements(name:Object = *):XMLList
name parameter
is optional. The name parameter can be a QName object, a String data type,
or any other data type that is then converted to a String data type. Use the name parameter to list a specific element. For example,
the element "first" returns "John" in this example:
<first>John</first>.
To list all elements, use the asterisk (*) as the parameter. The asterisk is also the default parameter.
Use the length() method with the asterisk parameter to output the total
number of elements. For example, numElement = addressbook.elements("*").length().
name:Object (default = *) — The name of the element. An element's name is surrounded by angle brackets.
For example, "first" is the name in this example:
<first></first>.
|
XMLList —
An XMLList object of the element's content. The element's content falls between the start and
end tags. If you use the asterisk (*) to call all elements, both the
element's tags and content are returned.
|
elements() method returns a
list of elements only
not comments, text properties, or processing instructions:
var xml:XML =
<foo>
<!-- comment -->
<?instruction ?>
text
<a>1</a>
<b>2</b>
</foo>;
trace(xml.elements("*").length()); // 2
trace(xml.elements("*")[0].toXMLString()); // <a>1</a>
trace(xml.elements("b").length()); // 1
trace(xml.elements("b")[0].toXMLString()); // <b>2</b>
elements() method instead of the XML dot (.) operator,
as in the following example:
var xml:XML =
<student id="206">
<class name="Trigonometry" />
<class name="Spanish 2" />
</student>
trace(xml.elements("class"));
| XML.child(), XMLList class, XML.length(), XML.attribute(), XML dot (.) operator |
public function hasComplexContent():Boolean
Boolean —
If the XML object contains complex content, true; otherwise false.
|
a that has
simple content and one property named a that has complex content:
var xml:XML =
<foo>
<a>
text
</a>
<a>
<b/>
</a>
</foo>;
trace(xml.a[0].hasComplexContent()); // false
trace(xml.a[1].hasComplexContent()); // true
trace(xml.a[0].hasSimpleContent()); // true
trace(xml.a[1].hasSimpleContent()); // false
| XML.hasSimpleContent() |
public function hasOwnProperty(p:String):Boolean
p parameter.
Parameters
p:String — The property to match.
|
Boolean —
If the property exists, true; otherwise false.
|
hasOwnProperty() method to ensure
that a property (b) exists prior to evaluating an expression (b == "11") that uses the
property:
var xml:XML =
<foo>
<a />
<a>
<b>10</b>
</a>
<a>
<b>11</b>
</a>
</foo>;
trace(xml.a.(hasOwnProperty("b") && b == "11"));
a does not have a property named b:
hasOwnProperty() method to ensure
that a property (item) exists prior to evaluating an expression
(item.contains("toothbrush")) that uses the
property:
var xml:XML =
<orders>
<order id='1'>
<item>toothbrush</item>
<item>toothpaste</item>
</order>
<order>
<returnItem>shoe polish</returnItem>
</order>
</orders>;
trace(xml.order.(hasOwnProperty("item") && item.contains("toothbrush")));
public function hasSimpleContent():Boolean
Boolean —
If the XML object contains simple content, true; otherwise false.
|
a that has
simple content and one property named a that has complex content:
var xml:XML =
<foo>
<a>
text
</a>
<a>
<b/>
</a>
</foo>;
trace(xml.a[0].hasComplexContent()); // false
trace(xml.a[1].hasComplexContent()); // true
trace(xml.a[0].hasSimpleContent()); // true
trace(xml.a[1].hasSimpleContent()); // false
| XML.hasComplexContent() |
public function inScopeNamespaces():Array
Array —
An array of Namespace objects.
|
public function insertChildAfter(child1:Object, child2:Object):*
child2 parameter after the child1 parameter in this XML object and returns the
resulting object. If the child1 parameter is null, the method
inserts the contents of child2 before all children of the XML object
(in other words, after none). If child1 is provided, but it does not
exist in the XML object, the XML object is not modified and undefined is
returned.
If you call this method on an XML child that is not an element (text, attributes, comments, pi, and so on)
undefined is returned.
child1:Object — The object in the source object that you insert before child2.
|
|
child2:Object — The object to insert.
|
* — The resulting XML object or undefined.
|
var xml:XML =
<menu>
<item>burger</item>
<item>soda</item>
</menu>;
xml.insertChildAfter(xml.item[0], <saleItem>fries</saleItem>);
trace(xml);
trace() output is the following:
<menu>
<item>burger</item>
<saleItem>fries</saleItem>
<item>soda</item>
</menu>
| XML.insertChildBefore() |
public function insertChildBefore(child1:Object, child2:Object):*
child2 parameter before the child1 parameter
in this XML object and returns the resulting object. If the child1 parameter
is null, the method inserts the contents of
child2 after all children of the XML object (in other words, before
none). If child1 is provided, but it does not exist in the XML object,
the XML object is not modified and undefined is returned.
If you call this method on an XML child that is not an element (text, attributes,
comments, pi, and so on) undefined is returned.
child1:Object — The object in the source object that you insert after child2.
|
|
child2:Object — The object to insert.
|
* — The resulting XML object or undefined.
|
var xml:XML =
<menu>
<item>burger</item>
<item>soda</item>
</menu>;
xml.insertChildBefore(xml.bar[0], <saleItem>fries</saleItem>);
trace(xml);
trace() output is the following:
<menu>
<saleItem>fries</saleItem>
<item>burger</item>
<item>soda</item>
</menu>
| XML.insertChildAfter() |
public function length():int
1.
The length() method of the XMLList class returns a value of 1 for
an XMLList object that contains only one value.
Returns
int —
Always returns 1 for any XML object.
|
public function localName():Object
Object —
The local name as either a String or null.
|
localName() method:
var xml:XML =
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:wx = "http://example.com/weather">
<wx:forecast>
<wx:city>Quito</wx:city>
</wx:forecast>
</soap:Body>
</soap:Envelope>;
trace(xml.localName()); // Envelope
public function name():Object
Object —
The qualified name is either a QName or null.
|
name() method to get the qualified
name of an XML object:
var xml:XML =
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:wx = "http://example.com/weather">
<wx:forecast>
<wx:city>Quito</wx:city>
</wx:forecast>
</soap:Body>
</soap:Envelope>;
trace(xml.name().localName); // Envelope
trace(xml.name().uri); // "http://www.w3.org/2001/12/soap-envelope"
name() method called on an XML property,
on a text element, and on an attribute:
var xml:XML =
<foo x="15" y="22">
text
</foo>;
trace(xml.name().localName); // foo
trace(xml.name().uri == ""); // true
trace(xml.children()[0]); // text
trace(xml.children()[0].name()); // null
trace(xml.attributes()[0]); // 15
trace(xml.attributes()[0].name()); // x
| XML.attributes(), operators.html#attribute_identifier |
public function namespace(prefix:String = null):*
prefix parameter is specified, the method returns the namespace
that matches the prefix parameter and that is in scope for the XML object. If there is no
such namespace, the method returns undefined.
Parameters
prefix:String (default = null) — The prefix you want to match.
|
* — Returns null, undefined or a namespace.
|
namespace() method
to get the namespace of an XML object and assign it to a Namespace object named soap
which is then used in identifying a property of the xml object
(xml.soap::Body[0]):
var xml:XML =
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:wx = "http://example.com/weather">
<wx:forecast>
<wx:city>Quito</wx:city>
</wx:forecast>
</soap:Body>
</soap:Envelope>;
var soap:Namespace = xml.namespace();
trace(soap.prefix); // soap
trace(soap.uri); // http://www.w3.org/2001/12/soap-envelope
var body:XML = xml.soap::Body[0];
trace(body.namespace().prefix); // soap
trace(xml.namespace().uri); // http://www.w3.org/2001/12/soap-envelope
trace(body.namespace("wx").uri); // "http://example.com/weather"
namespace() method to get the
default namespace for a node, as well as the namespace for a specific prefix ("dc":
var xml:XML =
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://purl.org/rss/1.0/">
<!-- ... -->
</rdf:RDF>;
trace(xml.namespace()); // http://www.w3.org/1999/02/22-rdf-syntax-ns#
trace(xml.namespace("dc")); // http://purl.org/dc/elements/1.1/
trace(xml.namespace("foo")); // undefined
public function namespaceDeclarations():Array
Array —
An array of Namespace objects.
|
var xml:XML =
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://purl.org/rss/1.0/">
<!-- ... -->
</rdf:RDF>;
for (var i:uint = 0; i < xml.namespaceDeclarations().length; i++) {
var ns:Namespace = xml.namespaceDeclarations()[i];
var prefix:String = ns.prefix;
if (prefix == "") {
prefix = "(default)";
}
trace(prefix + ":" , ns.uri);
}
trace() output is the following:
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
dc: http://purl.org/dc/elements/1.1/
(default): http://purl.org/rss/1.0/
| XML.namespace() |
public function nodeKind():String
String —
The node type used.
|
var xml:XML =
<example id="10">
<!-- this is a comment -->
<?test this is a pi ?>
and some text
</example>
trace (x.nodeKind());
trace (x.child(0).nodeKind());
trace (x.child(1).nodeKind());
trace (x.child(2).nodeKind());
trace (x.@id.nodeKind());
| Operators.html#attribute_identifier |
public function normalize():XML
XML —
The resulting normalized XML object.
|
normalize() method:
var xml:XML = <body></body>;
xml.appendChild("hello");
xml.appendChild(" world");
trace(xml.children().length()); // 2
xml.normalize();
trace(xml.children().length()); // 1
public function parent():*
undefined.
Returns
* — The parent XML object. Returns either a String or undefined.
|
parent() method to identify the parent element
of a specific element in an XML structure:
var xml:XML =
<body>
<p id="p1">Hello</p>
<p id="p2">Test:
<ul>
<li>1</li>
<li>2</li>
</ul>
</p>
</body>;
var node:XML = xml.p.ul.(li.contains("1"))[0]; // == <ul> ... </ul>
trace(node.parent().@id); // p2
public function prependChild(value:Object):XML
child object into the XML element before any existing XML
properties for that element.
Parameters
value:Object — The object to insert.
|
XML —
The resulting XML object.
|
prependChild() method to add an element to the
begining of a child list of an XML object:
var xml:XML =
<body>
<p>hello</p>
</body>;
xml.prependChild(<p>world</p>);
trace(xml.p[0].toXMLString()); // world
trace(xml.p[1].toXMLString()); // hello
public function processingInstructions(name:String = "*"):XMLList
name parameter is provided, lists all the children of the XML object
that contain processing instructions with that name. With no parameters, the method
lists all the children of the XML object that contain any processing instructions.
Parameters
name:String (default = "*") — The name of the processing instructions to match.
|
XMLList —
A list of matching child objects.
|
processingInstructions() method to get an
array of processing instructions for an XML object:
XML.ignoreProcessingInstructions = false;
var xml:XML =
<body>
foo
<?xml-stylesheet href="headlines.css" type="text/css" ?>
<?instructionX ?>
</body>;
trace(xml.processingInstructions().length()); // 2
trace(xml.processingInstructions()[0].name()); // xml-stylesheet
public function propertyIsEnumerable(p:String):Boolean
p is in the set of properties that can be iterated in a
for..in statement applied to the XML object. Returns true only
if toString(p) == "0".
Parameters
p:String — The property that you want to check.
|
Boolean —
If the property can be iterated in a for..in statement, true;
otherwise, false.
|
propertyNameIsEnumerable() method returns a value of
true only for the value 0; whereas for an
XMLList object, the return value is true for each valid index
value for the XMLList object:
var xml:XML =
<body>
<p>Hello</p>
<p>World</p>
</body>;
trace(xml.propertyIsEnumerable(0)); // true
trace(xml.propertyIsEnumerable(1)); // false
for (var propertyName:String in xml) {
trace(xml[propertyName]);
}
var list:XMLList = xml.p;
trace(list.propertyIsEnumerable(0)); // true
trace(list.propertyIsEnumerable(1)); // true
trace(list.propertyIsEnumerable(2)); // false
for (var propertyName:String in list) {
trace(list[propertyName]);
}
public function removeNamespace(ns:Namespace):XML
removeNamespaces()
method does not remove a namespace if it is referenced by the object's qualified name or the
qualified name of the object's attributes.
Parameters
ns:Namespace — The namespace to remove.
|
XML —
A copy of the resulting XML object.
|
var xml:XML =
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://purl.org/rss/1.0/">
<!-- ... -->
</rdf:RDF>;
trace(xml.namespaceDeclarations().length); // 3
trace(xml.namespaceDeclarations()[0] is String); //
var dc:Namespace = xml.namespace("dc");
xml.removeNamespace(dc);
trace(xml.namespaceDeclarations().length); // 2
public function replace(propertyName:Object, value:XML):XML
propertyName parameter
with the given value parameter.
If no properties match propertyName, the XML object is left unmodified.
Parameters
propertyName:Object — Can be a
numeric value, an unqualified name for a set of XML elements, a qualified name for a set of
XML elements, or the asterisk wildcard ("*").
Use an unqualified name to identify XML elements in the default namespace.
|
|
value:XML — The replacement value. This can be an XML object, an XMLList object, or any value
that can be converted with toString().
|
XML —
The resulting XML object, with the matching properties replaced.
|
replace() method
with an integer as the first parameter:
var xml:XML =
<body>
<p>Hello</p>
<p>World</p>
<hr/>
</body>;
xml.replace(1, <p>Bob</p>);
trace(xml);
trace() output:
<body>
<p>Hello</p>
<p>Bob</p>
<hr/>
</body>
replace() method
with a string as the first parameter:
var xml:XML =
<body>
<p>Hello</p>
<p>World</p>
<hr/>
</body>;
xml.replace("p", <p>Hi</p>);
trace(xml);
trace() output:
<body>
<p>Hi</p>
<hr/>
</body>;
replace() method
with a QName as the first parameter:
var xml:XML =
<body>
<p>Hello</p>
<p>World</p>
<hr/>
</body>;
var qname:QName = xml.p[0].name();
xml.replace(qname, <p>Bob</p>);
trace(xml);
trace() output:
<body>
<p>Hello</p>
<p>Bob</p>
<hr/>
</body>;
replace() method
with the string "" as the first parameter:
var xml:XML =
<body>
<p>Hello</p>
<p>World</p>
<hr/>
</body>;
xml.replace("*", <img src = "hello.jpg"/>);
trace(xml);
trace() output:
<body> <img src="hello.jpg"/> </body>
public function setChildren(value:Object):XML
value parameter.
Parameters
value:Object — The replacement XML properties. Can be a single XML object or an XMLList object.
|
XML —
The resulting XML object.
|
setChildren() method, first
using an XML object as the parameter, and then using an XMLList object as the parameter:
var xml:XML =
<body>
<p>Hello</p>
<p>World</p>
</body>;
var list:XMLList = xml.p;
xml.setChildren(<p>hello</p>);
trace(xml);
// <body>
// <p>hello</p>
// </body>
xml.setChildren(list);
trace(xml);
// <body>
// <p>Hello</p>
// <p>World</p>
// </body>
public function setLocalName(name:String):void
name parameter.
Parameters
name:String — The replacement name for the local name.
|
setLocalName() method
to change the local name of an XML element:
var xml:XML =
<ns:item xmlns:ns="http://example.com">
toothbrush
</ns:item>;
xml.setLocalName("orderItem");
trace(xml.toXMLString()); // <ns:orderItem xmlns:ns="http://example.com">toothbrush</ns:orderItem>
public function setName(name:String):void
name:String — The new name for the object.
|
setName() method
to change the name of an XML element:
var xml:XML =
<item>
toothbrush
</item>;
xml.setName("orderItem");
trace(xml.toXMLString()); // <orderItem>toothbrush</orderItem>
public function setNamespace(ns:Namespace):void
ns:Namespace — The new namespace.
|
soap namespace defined in one XML object
and applies it to the namespace of another XML object (xml2:
var xml1:XML =
<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<!-- ... -->
</soap:Envelope>;
var ns:Namespace = xml1.namespace("soap");
var xml1:XML =
<Envelope>
<Body/>
</Envelope>;
xml1.setNamespace(ns);
trace(xml1);
// <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">
// <Body/>
// </soap:Envelope>>;
public static function setSettings(... rest):void
ignoreComments,
ignoreProcessingInstructions, ignoreWhitespace,
prettyIndent, and prettyPrinting.
The following are the default settings, which are applied if no setObj parameter
is provided:
XML.ignoreComments = trueXML.ignoreProcessingInstructions = trueXML.ignoreWhitespace = trueXML.prettyIndent = 2XML.prettyPrinting = trueNote: You do not apply this method to an instance of the XML class; you apply it to
XML, as in the following code: XML.setSettings().
... rest — rest An object with each of the following properties:
|
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
var customSettings:Object = XML.settings();
var xml1:XML =
<foo>
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml1.toXMLString());
// <foo>
// <!-- comment -->
// <?instruction ?>
// </foo>
XML.setSettings(XML.defaultSettings());
var xml2:XML =
<foo>
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml2.toXMLString());
| ignoreComments, ignoreProcessingInstructions, ignoreWhitespace, prettyIndent, prettyPrinting, defaultSettings(), settings() |
public static function settings():Object
ignoreComments,
ignoreProcessingInstructions, ignoreWhitespace,
prettyIndent, and prettyPrinting.
Returns
Object —
An object with the following XML properties:
|
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
var customSettings:Object = XML.settings();
var xml1:XML =
<foo>
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml1.toXMLString());
// <foo>
// <!-- comment -->
// <?instruction ?>
// </foo>
XML.setSettings(XML.defaultSettings());
var xml2:XML =
<foo>
<!-- comment -->
<?instruction ?>
</foo>;
trace(xml2.toXMLString());
| XML.ignoreComments, XML.ignoreProcessingInstructions, XML.ignoreWhitespace, XML.prettyIndent, XML.prettyPrinting, XML.defaultSettings(), XML.setSettings() |
public function text():XMLList
XMLList —
The list of properties.
|
text() method to get the text nodes of
an XML object:
var xml:XML =
<body>
text1
<hr/>
text2
</body>;
trace(xml.text()[0]); // text1
trace(xml.text()[1]); // text2
public function toString():String
toString() returns the String contents of the
XML object with the following stripped out: the start tag, attributes, namespace declarations, and
end tag.toString() returns an XML encoded String
representing the entire XML object, including the start tag, attributes, namespace declarations,
and end tag.To return the entire XML object every time, use toXMLString().
String —
The string representation of the XML object.
|
toString() method returns when the
XML object has simple content:
var test:XML = <type name="Joe">example</type>; trace(test.toString()); //example
toString() method returns when the
XML object has complex content:
var test:XML =
<type name="Joe">
<base name="Bob"></base>
example
</type>;
trace(test.toString());
// <type name="Joe">
// <base name="Bob"/>
// example
// </type>
| XML.hasSimpleContent(), XML.hasComplexContent(), XML.toXMLString() |
public function toXMLString():String
toString() method,
the toXMLString() method always returns the start tag, attributes,
and end tag of the XML object, regardless of whether the XML object has simple content or complex
content. (The toString() method strips out these items for XML objects that contain
simple content.)
Returns
String —
The string representation of the XML object.
|
toString() method
(which is applied to all parameters of a trace() method, by default) and using the
toXMLString() method:
var xml:XML =
<p>hello</p>;
trace(xml); // hello
trace(xml.toXMLString()); // <p>hello</p>
| XML.toString() |
public function valueOf():XML
XML —
Returns the primitive value of an XML instance.
|
toValue() method
is the same as the source XML object:
var xml:XML = <p>hello</p>; trace(xml.valueOf() === xml); // true
@) symbol is used in several of the trace() calls to locate information
by tag name.
package {
import flash.display.Sprite;
public class XmlExample extends Sprite {
public function XmlExample() {
var employees:XML =
<employees>
<employee ssn="123-123-1234">
<name first="John" last="Doe"/>
<address>
<street>11 Main St.</street>
<city>San Francisco</city>
<state>CA</state>
<zip>98765</zip>
</address>
</employee>
<employee ssn="789-789-7890">
<name first="Mary" last="Roe"/>
<address>
<street>99 Broad St.</street>
<city>Newton</city>
<state>MA</state>
<zip>01234</zip>
</address>
</employee>
</employees>;
trace(employees.employee[0].address.zip); // 98765
trace(employees.employee[1].@ssn); // 789-789-7890
trace(employees.employee.name); // <name first="John" last="Doe"/>
// <name first="Mary" last="Roe"/>
trace(employees..zip[0]); // 98765
trace(employees..@ssn[1]); // 789-789-7890
trace(employees..name); // <name first="John" last="Doe"/>
// <name first="Mary" last="Roe"/>
trace(employees.employee[0].address.*); // <street>11 Main St.</street>
// <city>San Francisco</city>
// <state>CA</state>
// <zip>98765</zip>
var node:String = "zip";
trace(employees.employee[0].address[node]); // 98765
var attribute:String = "ssn";
trace(employees.employee[1].@[attribute]); // 789-789-7890
for each (var num:XML in employees..@ssn) {
trace(num); // 123-123-1234
} // 789-789-7890
var ssnToFind:String = "789-789-7890";
trace(employees.employee.(@ssn == ssnToFind).toXMLString());
// <employee ssn="789-789-7890">
// <name first="Mary" last="Roe"/>
// <address>
// <street>99 Broad St.</street>
// <city>Newton</city>
// <state>MA</state>
// <zip>01234</zip>
// </address>
// </employee>
}
}
}