Skip to main content
Participant
November 2, 2006
Question

XML parsing in flash

  • November 2, 2006
  • 2 replies
  • 228 views
Hi,

I'm using the following technique inorder to parse the XML like
this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;....

Just want to known if there is any way in Macromedia flash where in we can you element by names for parsing XML like asdk supported api : element.getElement("name"); instead of indexing.

<?xml version="1.0"?>
<inventors>
<person>
<name>Thomas Edison</name>
<comment>Inventor of cool stuff.</comment>
</person>
<person>
<name>Doug Engelbart</name>
<comment>Invented the Mouse</comment>
</person>
</inventors>


function loadXML(loaded) {
if (loaded) {
_root.inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
_root.comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
name_txt.text = _root.inventor;
comment_txt.text = _root.comments;
} else {
trace("file not loaded!");
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("inventors.xml"); element.getElement(OneCardXMLConstants.CARD_LEVEL_NFC_MESSAGE);


Thanks
This topic has been closed for replies.

2 replies

Participating Frequently
November 2, 2006
Or an AS2 solution you could use now is...

You could also use the XPathAPI class. Look in the Help files for a link to a document to show the methods available. Below I posted a little sample of code to get a selected node:

import mx.xpath.XPathAPI;

// Assume you have loaded an XML doc in the variable myXML

var myNode:XMLNode = XPathAPI.selectSingleNode(myXML.firstChild, "/process/step[@id=2]");

This will search through a XML document of the form

<?xml version="1.0" encoding="UTF-8"?>
<process>
<step id="1">
// some other tags
</step>
<step id="2">
//some other tags
</step>
</process>

and return the <step id="2"> node with all of its children nodes.

Tim
derobinson
Participating Frequently
November 2, 2006
Only if you're willing to use AS3 (Flash Player 9 only). AS3 supports ECMAScript for XML (E4X). that will get you what you want. Read more here:

http://www.adobe.com/devnet/actionscript/articles/actionscript3_overview.html

Cheers!