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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程