PHP:如何将 XML 数据存储在数组中?
下面是我正在使用的 XML - 还有更多项目 - 这是第一组。如何将这些元素放入数组中?我一直在尝试使用 PHP 的 SimpleXML 等,但我就是做不到。
<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 新手,所以我认为操作数组更容易。
Below is the XML I am working with - there are more items - this is the first set. How can I get these elements in to an array? I have been trying with PHP's SimpleXML etc. but I just cant do it.
<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>
Thanks guys
EDIT: I want the first items data in easy to access variables, I've been struggling for a couple of days to get SimpleXML to work as I am new to PHP, so I thought manipulating an array is easier to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么你想要它们在一个数组中?它们已经结构化,可以直接将它们用作 XML。
有
SimpleXML
和DOMDocument
,现在取决于您想要做什么数据(你没有提到这一点)哪一个更适合你。扩展您的问题以获取代码示例。编辑:以下是如何使用 SimpleXML 处理文档的示例:
Why do you want them in an array? They are structured already, use them as XML directly.
There is
SimpleXML
andDOMDocument
, now it depends on what you want to do with the data (you failed to mention that) which one serves you better. Expand your question to get code samples.EDIT: Here is an example of how you could handle your document with SimpleXML:
也许您应该看看 SimplePie,它将 XML feed 解析为数组或对象(好吧,两者之一: D)。我认为它也适用于命名空间和属性。
一些好处包括它的 GPL 许可证(免费)和它的支持社区。
Perhaps you should look at SimplePie, it parses XML feeds into an array or an object (well, one of the two :D). I think it works well for namespaces and attributes too.
Some benefits include it's GPL license (it's free) and it's support community.
SimpleXML 是读/写 XML 文件的最佳方式。通常它就像使用数组一样简单,但在您的情况下除外,因为涉及 XML 名称空间并且它使事情变得复杂。另外,用于存储属性的格式有点糟糕,因此它不是易于使用且显而易见,而是有点复杂,所以这就是您正在寻找的内容,以便您可以继续做一些对您来说更有趣的事情:
您'
$items
数组中包含您需要的所有内容,请使用print_r()
查看里面的内容。$url
应该是柠檬免费 API 的 URL。该代码假定一个属性不能有多个值(例如多个图像)。祝你好运。
SimpleXML is the best way to read/write XML files. Usually it's as easy as using arrays, except in your case because there's XML namespaces involved and it complicates stuff. Also, the format used to stored attributes kind of sucks, so instead of being easy to use and obvious it's kind of complicated, so here's what you're looking for so that you can move on to doing something more interesting for you:
You'll have everything you need in the
$items
array, useprint_r()
to see what's inside.$url
should be the URL of that lemonfree API thing. The code assumes there can't be multiple values for one attribute (e.g. multiple images.)Good luck.
这是我将从客户电子邮件地址簿系统返回的 XML 解析为数组的方法,以便我可以在页面上使用它。我认为使用的 XML 解析器是 PHP 的一部分。
这是它的文档 http://www.php.net/manual/en/ref .xml.php
如果你 var_dump($values) 你应该看到你的信息所在的数据是什么级别,我认为上面的 XML 是 4,所以你可以过滤掉你不知道的任何内容不想通过将 $value['level']==x 的值更改为所需的级别,即。 $value['level']==4。
这应该返回 $xml_array 作为数组,其中 $xml_array['title'] = 'Used 2005 Ford Mustang V6 Deluxe' 等。
希望对一些人有所帮助
This is the way I parsed XML returned from a clients email address book system into an array so I could use it on a page. uses an XML parser that is part of PHP, I think.
here's it's documentation http://www.php.net/manual/en/ref.xml.php
If you var_dump($values) you should see what level the data you're info is on, I think it'll be 4 for the above XML, so you can then filter out anything you don't want by changing the value of $value['level']==x to the required level, ie. $value['level']==4.
This should return $xml_array as an array with $xml_array['title'] = 'Used 2005 Ford Mustang V6 Deluxe' etc.
Hope that helps some