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,所以我认为操纵一个数组更容易。

+0

如果你告诉(相关部分),你有这么远的SimpleXML代码,人们可以指出你去过的地方错了。我认为尝试将它作为XML工作,而不是将其压缩成哑数组非常有用。 – Tomalak 2010-04-06 08:54:07

+0

如果您只需要处理数据,那么最好使用它作为simplexml对象,如果您愿意,可以像数组一样操作它。 – null 2010-04-07 18:52:14

也许你应该看看SimplePie,它将XML提要解析为数组或对象(以及其中的一个:D)。我认为它也适用于名称空间和属性。

一些好处包括它是GPL许可证(它是免费的),它是支持社区。

+0

给定的XML如何是一个新闻源? – Gordon 2010-04-06 08:50:45

+0

这里是用于检索xml的链接:http://api.lemonfree.com/listings?key=fd72eb830bafd4b1b766c2be38a136ab&make=ford&model=mustang – tommy 2010-04-06 08:54:05

+0

@tommy:在互联网上公开分享您的API密钥并不是一个好主意。如果我是你,我会删除上面的评论。 – Tomalak 2010-04-06 09:37:37

为什么你想要他们在一个数组?它们已经结构化,直接将它们用作XML。

SimpleXMLDOMDocument,现在取决于你想要对数据做什么(你没有提到这一点),哪一个更好地为你服务。展开您的问题以获取代码示例。


编辑:这里是你怎么可以用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]); 
} 
+0

我想让第一项数据容易访问变量,我一直在挣扎几天以让SimpleXML工作,因为我是PHP新手,所以我认为操纵数组更容易。 – tommy 2010-04-06 08:53:39

+0

@tommy:请将您的非工作SimpleXML代码的小代码样本放在一起,并将其发布到您的问题中。你离解决方案不远,我怀疑XML命名空间是唯一的解决方案。 – Tomalak 2010-04-06 09:02:49

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豪华'等。

希望帮助一些