tags:

views:

29

answers:

1

I'm trying to get some information (itemID, title, price and mileage) for multiple listings from eBay website using their api. So far I got this up

I've saved the document as .xml file using PHP cUrl and now I need to get/extract the values(itemID, title, price and mileage) into arrays and store them in database.

Unfortunately I never worked with PHP DOM, and I can't figure out how to extract the values. I tried to follow the tutorial found on IBM website but I had no success. Some help would be highly appreciated.

A: 

Here is a code sample to get the ItemIDs from your XML file:

<?php
$doc = new DomDocument();
$doc->load('a.xml');
$response = $doc->getElementsByTagName('GetMultipleItemsResponse')->item(0);
$items = $doc->getElementsByTagName('Item');
foreach ($items as $item)
{
    $itemID = $item->getElementsByTagName('ItemID')->item(0)->nodeValue;
    echo $itemID."\n";
}
?>
  • $doc->getElementsByTagName() returns a DomNodeList. It can be used in a foreach loop, and it has an item() method.
  • A DomNodeList will contain all matching elements. If you know there is only one element, you can retrieve that with ->item(0)
  • The item() method returns a DomElement, which inherits from DomNode. DomNode has a nodeValue property, which contains the text within the element.

Summary: use SimpleXML

Sjoerd