views:

302

answers:

4

Hi, In my flash, the socket server returns some xml data that I need to parse, but it seems that the only way to start the XML object is with the XML.Load() which loads an external file, but my XML file is already loaded into a variable

trace("raw: "+msg); msgArea.htmlText += "
update remote player loc"; var playerLocXMLOb = new XML(msg); playerLocXMLOb.ignoreWhite = true;

trace(playerLocXMLOb.firstChild.firstChild.nodeValue);

Which just returns

raw: <ploc><x>348</x><y>468</y><uid>red</uid></ploc>
null

Do you know what I am doing wrong? Or is an external file the only way?

+1  A: 

No you're doing it correctly, I would try

trace(playerLocXMLOb.x);

AS has some very strange things with XML and you can actually access a node by treating it as a member variable. Give that a shot and see what happens.

Chris Thompson
Nope, it returns undefined, so does ....ploc.uid and ....uidSurely there is a way to do this?
Cal S
Make sure you are defining the variable correctly, you might want to add var playerLocXMLOb:XML = new XML(msg);
Chris Thompson
A: 

Not sure what was going wrong but here is what I used to fix:

docXML = new XML(msg);
 XMLDrop = docXML.childNodes;
 XMLSubDrop = XMLDrop[0].childNodes;
 _root.rem_x = (parseInt(XMLSubDrop[0].firstChild));
 _root.rem_y = (parseInt(XMLSubDrop[1].firstChild));
 _root.rem_name = (XMLSubDrop[2].firstChild);
Cal S
A: 

How about firstChild.nodeValue?

trace(playerLocXMLOb.firstChild.nodeValue);//should trace 438
Amarghosh
A: 

There is actually a further text XMLNode inside the "x" node - you want to get the nodeValue of this text node, rather than of the "x" node.

var msg:String = "<ploc><x>348</x><y>468</y><uid>red</uid></ploc>";
trace("raw: "+msg);
var playerLocXMLOb = new XML(msg);
trace(playerLocXMLOb.firstChild);//returns the root XMLNode named ploc
trace(playerLocXMLOb.firstChild.firstChild);//returns the child XMLNode named x
trace(playerLocXMLOb.firstChild.firstChild.firstChild);//returns the child XMLNode, which is a text node
trace(playerLocXMLOb.firstChild.firstChild.firstChild).nodeValue;//returns the String contents of that text node

When you trace a text XMLNode (playerLocXMLOb.firstChild.firstChild.firstChild) you only see a string, but it is in fact an object (whose toString method just returns its String contents)

tarling