dornc9470 2017-11-01 11:50
浏览 59
已采纳

如何使用PHP获取XML属性

This is my xml file:

<device name=" name ">
<interface>
    <ip > someip </ip>
</interface>
<display>
    <name> \\.\DISPLAY1 ( 1920 X 1080 )</name>
</display>
<output>
    <audio> Speakers (High Definition Audio </audio>
    <audio> Digital Audio (S/PDIF) (High De </audio>
    <audio> Digital Audio (S/PDIF) (High De </audio>
</output>

And this is my PHP code:

<body style="font-family: Arial">
<?php
    $xml=simplexml_load_file("device.xml");
    var_dump($xml);
    $attr = $xml->device->attributes()->name;
?>
<div id="leftDiv">
    <ul id="menu">
        <li><?php echo $attr; ?></li>
    </ul>
</div>
<div id="mainDiv">

</div>
<div id="rightDiv">

</div>

I want to reach device's name attribute and get its value, but I always get an error: main(): Node no longer exists in C:\xampp\htdocs\Project\index.php on line 30. Line 30 is this line: $attr = $xml->device->attributes()->name; What's wrong?

  • 写回答

1条回答 默认 最新

  • dongxi1879 2017-11-01 11:58
    关注

    php file code

    remove device from $xml->device->attributes()->name;

    <body style="font-family: Arial">
    <?php
        $xml=simplexml_load_file("device.xml");
        var_dump($xml);
        $attr = $xml->attributes()->name;
    ?>
    <div id="leftDiv">
        <ul id="menu">
            <li><?php echo $attr; ?></li>
        </ul>
    </div>
    <div id="mainDiv">
    
    </div>
    <div id="rightDiv">
    
    </div>
    

    xml file

    missing </device> to end of file so put it

    <device name=" name ">
    <interface>
        <ip > someip </ip>
    </interface>
    <display>
        <name> \\.\DISPLAY1 ( 1920 X 1080 )</name>
    </display>
    <output>
        <audio> Speakers (High Definition Audio </audio>
        <audio> Digital Audio (S/PDIF) (High De </audio>
        <audio> Digital Audio (S/PDIF) (High De </audio>
    </output>
    </device>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 给出完整的python代码并解释
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 关于#wpf#的问题:怎么更改LayoutGroup组件的标签页的字体颜色
  • ¥15 中级微观经济学,生产可能性边界问题