doujiao9574 2015-01-21 13:51
浏览 22
已采纳

XML内容转换为PHP变量

I have been at this for hours now and really can't get it working... I have the following content in an xml file:

<stores data="4850" times="01010101">
  <folder info="storage" DateTime="datetime1" update="212121012" versionNumber="ver1" url="http://url1" locater="location1"/>
  <folder info="images" DateTime="datetime2" update="1421748774" versionNumber="ver2" url="http://url2" locater="location2"/>
</stores data>

And I need to get each element into a different variable using PHP. This is the code that I have, which gets the xml file and prints it out, but after this I'm stuck.

$xml_ip = simplexml_load_file('file.xml');
print_r($xml_ip);

With this I get what looks like an array on the screen, but I can't get all of the xml entries into variables.

Thanks.

  • 写回答

1条回答 默认 最新

  • dongshan7060 2015-01-21 14:04
    关注

    That is not valid XML, if you make the XML valid it will work

    So change the file to

    <stores data="4850" times="01010101">
      <folder info="storage" DateTime="datetime1" update="212121012" versionNumber="ver1" url="http://url1" locater="location1"/>
      <folder info="images" DateTime="datetime2" update="1421748774" versionNumber="ver2" url="http://url2" locater="location2"/>
    </stores>
    

    All I did was fix this line

    </stores data>
    

    Copy/Paste will get you every time!!!

    Then you will get this :-

    SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [data] => 4850
                [times] => 01010101
            )
        [folder] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [info] => storage
                                [DateTime] => datetime1
                                [update] => 212121012
                                [versionNumber] => ver1
                                [url] => http://url1
                                [locater] => location1
                            )
                    )
                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [info] => images
                                [DateTime] => datetime2
                                [update] => 1421748774
                                [versionNumber] => ver2
                                [url] => http://url2
                                [locater] => location2
                            )
                    )
            )
    )
    

    Reply to additional comment

    You already have this data in a variable, this line

    $xml_ip = simplexml_load_file('file.xml');
    

    creates a PHP SimpleXMLElement object called $xml_ip

    You now need to learn how to deal with this object, here is the documentation

    And here is a simple bit of code to print out data as a head start.

    $xml_ip = simplexml_load_file('file.xml');
    
    echo $xml_ip->attributes()['data'] . PHP_EOL;
    echo $xml_ip->attributes()['times'] . PHP_EOL;
    
    foreach ( $xml_ip->folder as $xmlEltObj ) {
    
        foreach ($xmlEltObj->attributes() as $attr => $val) {
            echo '   '. $attr . " = " . $val.PHP_EOL;
        }
    
    }
    

    Which prints

    4850
    01010101
       info = storage
       DateTime = datetime1
       update = 212121012
       versionNumber = ver1
       url = http://url1
       locater = location1
       info = images
       DateTime = datetime2
       update = 1421748774
       versionNumber = ver2
       url = http://url2
       locater = location2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂