PHP中的XML/数组到键值对


XML/Array to Key value pair in PHP

我有以下输出:

<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>

我想以键值对的形式读取这个输出,然后想要存储每个值。例如,我想要name

的值

谁能帮我的代码..我没有得到如何做的线索。

可以使用simplexml扩展函数来实现您的目标。有了这个扩展,您可以从文件,从字符串或从html节点加载xml。按照链接获取有关每个加载函数的更多信息。我将在这里解释如何使用simplexml_load_file进行加载。这段代码:

<?php
echo "<pre>";   
$xml = simplexml_load_file("name.xml");
    print_r($xml);
    echo "</pre>";
?>

将返回这个:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [count] => 
            [time] => 0.011766910552979
        )
    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [id] => 607
                    [name] => MSPOT6071
                    [description] => Hip Hop / Raps
                    [type] => 3
                    [radio_folder_id] => SimpleXMLElement Object
                        (
                        )
                    [albumart] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
                    [albumart_300] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
                    [albumart_500] => http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
                )
            [1] => SimpleXMLElement Object
                (
                    [id] => 48542614
                    [name] => US Pop - TESTB
                    [description] => Blues 
                    [type] => 3
                    [radio_folder_id] => SimpleXMLElement Object
                        (
                        )
                )
        )
)

你想要的信息,可以这样访问:

<?php
echo "<pre>";   
$xml = simplexml_load_file("name.xml");
    print_r($xml);
    echo "</pre>";
    echo "<pre>";
    foreach($xml->children() as $item){
        $arr = get_object_vars($item);
        foreach($arr as $key=>$value){
            echo "$key => $value" . PHP_EOL;            
        }
    }
    echo "</pre>";
?>

注意,它将首先作为对象访问。然后,您可以让所有对象变量动态地浏览您的对象属性。

<?php
$xmlstr = '<?xml version="1.0" standalone="yes"?>
<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>';
$xml = new SimpleXMLElement($xmlstr);
foreach($xml->item as $item)
{
    echo $item->name."<br>";
}
echo $xml->item[0]->name;
echo '<pre>';
print_r($xml);
echo '</pre>';
?>

将XML字符串赋值给变量$xmlstr,这样就不会出现不完整的XML文档错误。请确保在XML文档的顶部包含以下内容。

<?xml version="1.0" standalone="yes"?> 

然后使用内置的SimpleXML类通过传递您的XML字符串$xmlstr到SimpleXML:

$xml = new SimpleXMLElement($xmlstr);

现在您可以使用SimpleXML类的属性和方法作为PHP对象访问XML文件。在这个例子中,我们循环遍历XML文档中的'item'(s)并打印出'name'元素:

foreach($xml->item as $item)
{
    echo $item->name."<br>";
}

我还包含了访问第一个item元素的代码:

echo $xml->item[0]->name;

和一些调试代码一样,在SimpleXML对象中查看XML文档:

echo '<pre>';
print_r($xml);
echo '</pre>';

通过名称访问键,或者在本例中访问对象属性。所以在foreach循环中你可以这样写:

if($item->name)
{
    echo $item->name;
}

if($item->description)
{
    echo $item->description;
}

愿原力与你同在。