【问题标题】:Xml.nodeValue causes "Bad node type" errorXml.nodeValue 导致“节点类型错误”错误
【发布时间】:2018-04-16 23:24:28
【问题描述】:
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);
        }
    }

}

输出:

Main.hx:23:6

Main.hx:27: 计数

Main.hx:28: 0

Main.hx:34:错误的节点类型,意外的 0

我无法完全理解nodeValue属性的操作原理。因此,我无法解决我的问题。这里有什么帮助吗?

附:我的配置是:Haxe + OpenFL 瞄准 Neko。

【问题讨论】:

    标签: xml exception haxe xmlnode neko


    【解决方案1】:

    elementsNamed() 返回 XmlType.Element 类型的节点,docs for nodeValue 明确声明:

    返回节点值。仅当 Xml 节点不是元素或文档时才有效。

    所以nodeValue 将适用于所有其他可能的XmlType 值。在您的情况下,您要检索的值存储在 XmlType.PCData 节点中,您可以使用 firstChild() 访问它:

    for (x in xml.elementsNamed("count"))
    {
        trace(x.firstChild().nodeType); // 1 - XmlType.PCData
        trace(x.firstChild().nodeValue); // 6
    }
    

    &lt;count&gt;6&lt;/count&gt; 的完整结构如下所示:

    [XmlType.Document] -> [XmlType.Element <count>] -> [XmlType.PCData 6]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      相关资源
      最近更新 更多