PHP:如何将 XML 数据存储在数组中?

发布于 2024-08-28 00:21:32 字数 1376 浏览 9 评论 0原文

下面是我正在使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中楼上月下 2024-09-04 00:21:32

为什么你想要它们在一个数组中?它们已经结构化,可以直接将它们用作 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] );
}

Why do you want them in an array? They are structured already, use them as XML directly.

There is SimpleXML and DOMDocument, 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:

$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] );
}
蓝眸 2024-09-04 00:21:32

也许您应该看看 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.

停顿的约定 2024-09-04 00:21:32

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 应该是柠檬免费 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:

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

You'll have everything you need in the $items array, use print_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.

饮惑 2024-09-04 00:21:32

这是我将从客户电子邮件地址簿系统返回的 XML 解析为数组的方法,以便我可以在页面上使用它。我认为使用的 XML 解析器是 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($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

$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'];
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文