我有以下输出:
<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“的值
有人能帮我写代码吗……我不知道该怎么做。
发布于 2013-05-14 09:10:43
您可以使用simplexml扩展函数来实现您的目标。通过这个扩展,您可以从file、从string或从html node加载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-14 09:19:20
这个
<?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文档中的“item”,并打印出“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;
}愿原力与你同在。
https://stackoverflow.com/questions/16533467
复制相似问题