PHP:如何将XML数据存储在数组中?
下面是我正在使用的XML - 有更多的项目 - 这是第一组。我如何将这些元素放入数组中?我一直在尝试使用PHP的SimpleXML等,但我不能这样做。PHP:如何将XML数据存储在数组中?
<response xmlns:lf="http://api.lemonfree.com/ns/1.0">
<lf:request_type>listing</lf:request_type>
<lf:response_code>0</lf:response_code>
<lf:result type="listing" count="10">
<lf:item id="56832429">
<lf:attr name="title">Used 2005 Ford Mustang V6 Deluxe</lf:attr>
<lf:attr name="year">2005</lf:attr>
<lf:attr name="make">FORD</lf:attr>
<lf:attr name="model">MUSTANG</lf:attr>
<lf:attr name="vin">1ZVFT80N555169501</lf:attr>
<lf:attr name="price">12987</lf:attr>
<lf:attr name="mileage">42242</lf:attr>
<lf:attr name="auction">no</lf:attr>
<lf:attr name="city">Grand Rapids</lf:attr>
<lf:attr name="state">Michigan</lf:attr>
<lf:attr name="image">http://www.lemonfree.com/images/stock_images/thumbnails/2005_38_557_80.jpg</lf:attr>
<lf:attr name="link">http://www.lemonfree.com/56832429.html</lf:attr>
</lf:item>
<!-- more items -->
</lf:result>
</response>
谢谢你们
编辑:我想简单的第一项数据访问的变量,我一直在挣扎了几天得到SimpleXML的工作,我是新来的PHP,所以我认为操纵一个数组更容易。
为什么你想要他们在一个数组?它们已经结构化,直接将它们用作XML。
有SimpleXML
和DOMDocument
,现在取决于你想要对数据做什么(你没有提到这一点),哪一个更好地为你服务。展开您的问题以获取代码示例。
编辑:这里是你怎么可以用SimpleXML处理文档的例子:
$url = "http://api.lemonfree.com/listings?key=xxxx&make=ford&model=mustang";
$ns_lf = "http://api.lemonfree.com/ns/1.0";
$response = simplexml_load_file($url);
// children() fetches all nodes of a given namespace
$result = $response->children($ns_lf)->result;
// dump the entire <lf:result> to see what it looks like
print_r($result);
// once the namespace was handled, you can go on normally (-> syntax)
foreach ($result->item as $item) {
$title = $item->xpath("lf:attr[@name='title']");
$state = $item->xpath("lf:attr[@name='state']");
// xpath() always returns an array of matches, hence the [0]
echo($title[0].", ".$state[0]);
}
SimpleXML的是读/写XML文件的最好方法。通常它和使用数组一样简单,除了你的情况,因为涉及到XML命名空间,并且使事情变得复杂。另外,用于存储属性的格式很糟糕,所以不是易于使用,而且显而易见,它有点复杂,所以这里是你要找的东西,这样你就可以继续为你做更有趣的事情:
$response = simplexml_load_file($url);
$items = array();
foreach ($response->xpath('//lf:item') as $item)
{
$id = (string) $item['id'];
foreach ($item->xpath('lf:attr') as $attr)
{
$name = (string) $attr['name'];
$items[$id][$name] = (string) $attr;
}
}
您将在$items
阵列中获得所需的全部内容,请使用print_r()
来查看内部内容。 $url
应该是那个lemonfree API的URL的东西。该代码假定一个属性不能有多个值(例如多个图像)。
祝你好运。
这是我解析从客户端电子邮件地址簿系统返回的XML到数组中的方式,所以我可以在页面上使用它。我认为使用的PHP解析器是PHP的一部分。
这里是它的文档http://www.php.net/manual/en/ref.xml.php
$user_info = YOUR_XML
SETS $xml_array AS ARRAY
$xml_array = array();
// SETS UP XML PARSER AND PARSES $user_info INTO AN ARRAY
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $user_info, $values, $index);
xml_parser_free($xml_parser);
foreach($values as $key => $value)
{
// $value['level'] relates to the nesting level of a tag, x will need to be a number
if($value['level']==x)
{
$tag_name = $value['tag'];
// INSERTS DETAILS INTO ARRAY $contact_array SETTING KEY = $tag_name VALUE = value for that tag
$xml_array[strtolower($tag_name)] = $value['value'];
}
}
如果你的var_dump($值),你应该看到的数据你的信息是,我觉得什么样的水平,它会为上面的XML是4,所以你然后可以通过将$ value ['level'] == x的值更改为所需的级别来过滤掉任何你不想要的东西,即。 $值[ '电平'] == 4。
这应该返回$ xml_array作为数组与$ xml_array ['标题'] ='使用2005年福特野马V6豪华'等。
希望帮助一些
如果你告诉(相关部分),你有这么远的SimpleXML代码,人们可以指出你去过的地方错了。我认为尝试将它作为XML工作,而不是将其压缩成哑数组非常有用。 – Tomalak 2010-04-06 08:54:07
如果您只需要处理数据,那么最好使用它作为simplexml对象,如果您愿意,可以像数组一样操作它。 – null 2010-04-07 18:52:14