doudang9147 2010-06-04 12:34
浏览 17
已采纳

通过父属性访问值[重复]

I am looking to get the value A-1 through xpath based on a passed attribue.

I have passed the index attribute of the unit through php from a previous page and am accessing it by global GET:

$value = intval($_GET['index']);

the xml:

<UNIT index='1'>
     <ID>A-1</ID>
     <MANUFACTURER>testing inc.</MANUFACTURER>
</UNIT>
<UNIT index='2'>
     <ID>A-2</ID>
     <MANUFACTURER>testing inc.</MANUFACTURER>
</UNIT>

I'm trying to echo it out using:

$xml = new SimpleXMLElement('demo.xml',NULL,true);

echo $xml->UNIT[$value]->ID;

I know i'm getting the "1" that I need passed through because I echo'd $value to check, but its giving me the ID of A-2, which would be the xml index number (starting from 0) - not my attribute index number.

</div>
  • 写回答

2条回答 默认 最新

  • duanputian5341 2010-06-04 12:39
    关注

    You can use the SimpleXMLElement::xpath method to query for the specific UNIT that you want with an XPath query like //UNIT[@index=2].

    $value = intval($_GET['index']);
    $xml   = new SimpleXMLElement('demo.xml',NULL,true);
    $units = $xml->xpath("//UNIT[@index=$value]"); // xpath returns an array
    if (isset($units[0])) {
        echo $units[0]->ID;
    } else {
        echo "No unit with index $value";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序