Name

XML.parseXML( ) Method — parse a string of XML source code

Availability

Flash 5

Synopsis

XMLdoc.parseXML(string)

Arguments

string

A string of XML source code.

Description

The parseXML( ) method is akin to an internal load( ) function; it reads and parses the XML source contained by string, converts that XML into an object hierarchy, and then places the resulting hierarchy into XMLdoc. Any previous contents of XMLdoc are replaced by the new hierarchy. XMLdoc must be an instance of the XML class, not the XMLnode class.

To include raw HTML or XML source code in a text node without parsing it, use a CDATA section as follows:

<![CDATA[ source ]]>

For example, the following code creates a MESSAGE element with a single child text node containing the text “<B>Welcome</B> to my site” (the <B> tag is not interpreted as an XML tag and does not become part of the XML object hierarchy):

myDoc = new XML( );
myDoc.parseXML("<MESSAGE><![CDATA[<B>Welcome</B> to my site]]></MESSAGE>");
trace(myDoc);  // Displays: "<MESSAGE><B>Welcome</B> to my site</MESSAGE>"

Example

We can use parseXML( ) as a means of replacing the current object hierarchy in an XML object with a new hierarchy based on internally composed XML source code (for example, some user input). In the following example, we create a simple XML message by combining markup with input from text fields named username and content:

myDoc = new XML( ); myXMLsource = "<MESSAGE><USER>" + username + "</USER><CONTENT>" + content + "</CONTENT></MESSAGE>"; myDoc.parseXML(myXMLsource); ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.