0

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.

3
  • simplexml is the easiest way of extracting XML data in PHP: php.net/simplexml_load_file Commented Jun 17, 2010 at 18:31
  • <pre> $doc = new DOMDocument(); $doc->load( 'api.xml' ); $items = $doc->getElementsByTagName("GetMultipleItemsResponse"); foreach( $GetMultipleItemsResponse as $Item ) { $itemID = $Item->getElementsByTagName( "ItemID" ); $itemID = $Item->item(0)->nodeValue; } echo $itemID; </pre> Commented Jun 17, 2010 at 18:45
  • that should be an answer instead of a comment. Commented Jun 17, 2010 at 18:47

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.