parsing XML from XML string for actionscript 3
I am trying to parse out the nodeName but nothing was return, what is wrong with my script?
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
import flash.xml.XMLNodeType;
var MaintainXML开发者_运维百科:XML =
<letter><to>senocular</to><body>Get a life</body></letter>;
trace("status"+MaintainXML.status); // traces "0" (No error)
trace("nodeName" + MaintainXML.firstChild.nodeName); // traces "letter"
trace("nodeName" + MaintainXML.firstChild.firstChild.nodeName); // traces "to"
trace("nodeValue"+MaintainXML.firstChild.firstChild.firstChild.nodeValue); // traces
You are trying to use methods or properties on your XML that are not existent: Neither status
, nor firstChild
, nor nodeName
are available for the XML Object. Flash will interpret those calls as a search query, so it looks for child nodes with the name "status", resp. "firstChild" as in
<root>
<status />
<firstChild />
</root>
and returns an empty XMLList, because of course it cannot find such nodes.
Also, your MaintainXMLs root node is <letter>
, so the first child would be <to>
.
Try this:
var MaintainXML : XML = <letter><to>senocular</to><body>Get a life</body></letter>;
trace( "nodeName:" + MaintainXML.to.name() ); // traces "to"
trace( "nodeValue:" + MaintainXML.to.toString() ); // traces "senocular"
精彩评论