class Main extends Sprite
{
public function new()
{
super();
try
{
var xml:Xml = Xml.parse("<count>6</count>");
trace(xml.nodeType);
for (x in xml.elementsNamed("count"))
{
trace(x.nodeName);
trace(x.nodeType);
trace(x.nodeValue);
}
}
catch (err:Dynamic)
{
trace(err);
Sys.exit(1);
}
}
}
Output:
Main.hx:23: 6
Main.hx:27: count
Main.hx:28: 0
Main.hx:34: Bad node type, unexpected 0
I can't fully understand the principle of operation of nodeValue property. Because of that, I can't solve my problem. Any help here?
P.S. My configuration is: Haxe + OpenFL targeting Neko.
elementsNamed() returns nodes of type XmlType.Element, and the docs for nodeValue explicitly state:
Returns the node value. Only works if the Xml node is not an Element or a Document.
So nodeValue will work for all other possible XmlType values. In your case, the value you want to retrieve is stored in a XmlType.PCData node, and you can access it using firstChild():
for (x in xml.elementsNamed("count"))
{
trace(x.firstChild().nodeType); // 1 - XmlType.PCData
trace(x.firstChild().nodeValue); // 6
}
The full structure of <count>6</count> looks like this:
[XmlType.Document] -> [XmlType.Element <count>] -> [XmlType.PCData 6]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With