【问题标题】:simpleXMLElement find nodesimpleXMLElement 查找节点
【发布时间】:2014-10-19 03:39:13
【问题描述】:

我在 xml 元素中有以下结构:

<assets>

    <icon id="1" type="grave" x="586" y="477">
        <text short="hello world" tooltip="" tteng=" "/>
    </icon>

</assets>

现在,使用PHP,我尝试使用xpath 找到这个特定的node。我必须通过&lt;text&gt; sn-p 中的短值找到这个节点。

所以,我需要得到父级

<text="hello world" ... >

我在看php.net,但它对我没有多大帮助。

我尝试的是这样的:

$node = $xml->xpath('//text="hello world"');

但这并没有成功。

关于来自 ma​​rc-b 的信息,我在我的函数中添加了以下内容:

public static function loadValuesFromNode($fileName, $name) {

    var_dump($name); // echoes "hello world"

    $xml = simplexml_load_file(self::$xmlDir . "/" . $fileName); // correct path, already tested
    $node = $xml->xpath('//text[@short="'.$name.'"]');

    var_dump($node); // empty -> (array(0) { }

}

如果我将 xpath 更改为:

'[@short="'.$name.'"'] (so without the text-thingy)

它返回“假”;

提前致谢:)

【问题讨论】:

    标签: php xml xpath simplexml


    【解决方案1】:

    要测试一个属性的内容,你需要

    //node[@attribute="value"]
    

    所以

    //text[@short="hello world"]
    

    【讨论】:

    • 返回空值:/我编辑了我的帖子,如果你想看看的话。非常感谢您的回答。
    • nvm,我很笨,忘记了 // - 谢谢 :)
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2012-02-26
    • 2015-11-11
    • 2017-02-14
    • 2013-07-13
    • 1970-01-01
    相关资源
    最近更新 更多