du1068 2019-05-03 17:48
浏览 43
已采纳

使用SimpleXml从xml文件中提取数据

Hello I am trying to extract data from an XML file using SimpleXml but it's not extracting the LA 1 from display-name tag

This is the XML :

<tv generator-info-name="munich" generator-info-url="http://test.com">
    <channel id="254">
        <display-name lang="es">LA 1</display-name>
    </channel>
</tv>

This is my code which I am using to extract the id attribute from channel tag :

 $epg = simplexml_load_file("epg.xml") or die("Error: Cannot create object");
 foreach($epg->channel as $canal) {
     echo $canal[id];
 }
  • 写回答

2条回答 默认 最新

  • duanchi6397 2019-05-03 17:55
    关注

    As the name of the element you want to display has a - in it, you will need to output the element using (I cast it to a string just to be pedantic)...

    echo (string)$canal->{'display-name'};
    

    Using

    $canal->display-name
    

    will be interpreted as $canal->display minus name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 判断某个数 区间是否存在
  • ¥15 appium控制多个雷电模拟器问题
  • ¥15 C# iMobileDevice
  • ¥15 谁会做这个啊#ensp#Boson NetSim
  • ¥15 如何编写针对TPS6503320FRGE型号的电源管理芯片的编程代码?
  • ¥15 设计简单目录管理系统,要满足以下内容
  • ¥15 关于九十度混合耦合器信号分析问题
  • ¥15 Cesm如何关闭不用的模块呢
  • ¥15 vue2两层数据导出为excle
  • ¥15 有人能帮我做一下这个项目吗