tags:

views:

169

answers:

4

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"&gt;
  <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&lt;/lf:attr&gt; 
      <lf:attr name="link">http://www.lemonfree.com/56832429.html&lt;/lf:attr&gt; 
    </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.

A: 

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.

ILMV
And how is the given XML a news feed?
Gordon
tommy
@tommy: It is not a good idea to share your API key openly on the Internet. If I were you I would delete the above comment.
Tomalak
+1  A: 

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&amp;make=ford&amp;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] );
}
Tomalak
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.
tommy
@tommy: Please put together a small code sample of your non-working SimpleXML code and post it in your question. You can't be far from the solution, I suspect the XML namespace is the only holdup.
Tomalak
A: 

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.

Josh Davis
A: 

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

andy-score