douchao9899 2016-09-29 11:55
浏览 17
已采纳

在php中访问0索引xml属性

I have written a program which read an XML file and provide me an array. So I further perform my operations but I am not able to get the value of 0 index.

$xml = simplexml_load_file("app_string.xml");

foreach ($xml as $value) {
    echo "<pre>";
    print_r($value);
}

That is the output:

SimpleXMLElement Object (
    [@attributes] => Array
        (
            [name] => app_name
        )

    [0] => My Data )

I tried with

$value->{0} 

and

$value['0']

Not getting the desire data.

How can I get My Data from [0] => My Data

  • 写回答

1条回答 默认 最新

  • douxin2011 2016-09-29 11:57
    关注

    To retrieve the element data, you can simple cast the XML element to a string, as in:

    $xml = simplexml_load_file("app_string.xml");
    
    foreach ($xml as $value) {
        echo "<pre>";
        $data = (string)$value;
        echo $data;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题