duanmei1850 2014-04-01 11:28
浏览 20

使用PHP从Last XML元素中提取Last Data Tag

I have an XML file which I have used xpath to extract the last Element.

So at the moment this line of code below:

    //Get The file from directory
    $xml=simplexml_load_file("docs/03-24-2014.xml");

    //Get last Element
    $last = $xml->xpath("/DocumentElement/Datas[last()]");

Output

array(1) { [0]=> object(SimpleXMLElement)#4 (6) { ["Name"]=> string(12) "COM3-Screen1" ["AdjustMode"]=> string(10) "AutoAdjust" ["Time"]=> string(19) "03-24-2014 11:59:27" ["ScreenBrightnessValues_x0028_0-255_x0029_"]=> string(1) "2" ["AmbientBrightness_x0028_lux_x0029_"]=> string(1) "0" ["BrightnessPercentage"]=> string(2) "0%" } }

Goal

What am trying to achieve is extract the last data tag called "BrightnessPercentage and store it in a string"

I tried echo $last->BrightnessPercentage .; but it didnt work!

Any help you be kindly appreciated

  • 写回答

1条回答 默认 最新

  • donglan7594 2014-04-01 12:43
    关注

    To get that attribute directly from xpath as string, do:

    $brightness = (string)$xml->xpath("/DocumentElement/Datas[last()]/@BrightnessPercentage")[0];
    

    requires PHP >= 5.4. If you are on a lower version, do:

    $brightness = $xml->xpath("/DocumentElement/Datas[last()]/@BrightnessPercentage");
    $brightness = (string)$brightness[0];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥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的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比