1

我有以下输出:

<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>

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

谁能帮我写代码..我不知道该怎么做。

4

2 回答 2

2

您可以使用 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>";
?>

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

于 2013-05-14T01:10:43.473 回答
0

<?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"?> 

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

$xml = new SimpleXMLElement($xmlstr);

现在,您可以使用 SimpleXML 类的属性和方法将 XML 文件作为 PHP 对象访问。在本例中,我们遍历 XML 文档中的“项目”并打印出“名称”元素:

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;
}

愿原力与你同在。

于 2013-05-14T01:19:19.677 回答