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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?