dslh32311 2014-08-25 22:17
浏览 89
已采纳

simpleXMLElement找到节点

i have the following structure in an xml-element:

<assets>

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

</assets>

Now, using PHP, i try to find this specific node with xpath. I have to find this node by the short-value in the <text> snippet.

So, i need to get the parent of

<text="hello world" ... >

I was looking on php.net, but it didn't helped me that much.

What i tried is this:

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

but that didn't do the trick.

Regarding to the info from marc-b i added the following to my function:

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) { }

}

If i change the xpath to:

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

it returns "false";

Thanks in advance :)

  • 写回答

1条回答 默认 最新

  • dsg41888 2014-08-25 22:20
    关注

    To test an attribute's contents, you need

    //node[@attribute="value"]
    

    so

    //text[@short="hello world"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图