php将xml文件转化为数组:simplexml_load_string

<?php
$str = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
    <book>
        <category>COOKING</category>
        <title>Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book>
        <category>CHILDREN</category>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book>
        <category>WEB</category>
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>

    <news>
        <category>CHILDREN</category>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </news>
    <news>
        <category>WEB</category>
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </news>

    <blog>
        <category>CHILDREN</category>
        <title>Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </blog>
    <blog>
        <category>WEB</category>
        <title>Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </blog>
</note>
XML;

$strObj = simplexml_load_string($str);
$json = json_encode($strObj);
$ary = json_decode($json, true);

echo ‘<pre>‘;
print_r($ary);
echo ‘</pre>‘;

php将xml文件转化为数组:simplexml_load_string,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。