Solved

Extracting info from XML on PHP

Posted on 2011-03-20
5
447 Views
Last Modified: 2012-08-14
Hi I am trying to extract info from http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043 which is an xml. I currently cannot extract anything so I would like to ask for some help.

I am trying to run this code with no response:

<?php

$url = "http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043";

$xml = simplexml_load_file($url);

echo $xml->response->url;

?>

but actually I would like to print something like

item number 1:
   the id is 58774043
   the title is Usb Wifi Exteriores Gsky 1000mw + Antena 12dbi  + 5mts Cable
   and so on..
can someone help me please.
0
Comment
Question by:degaray
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35176200
Do not use response as the first root it is ommited,
use: echo $xml->url;
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35176213
<?php

$url = "http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043";

$xml = simplexml_load_file($url);

foreach ($xml->listing->items->item as $val){
    echo"$val->title<br />";
}

?>

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35177216
http://www.laprbass.com/RAY_temp_degaray.php

Outputs:
ELEMENT NUMBER 1
ITEM ID = 58774043
ITEM TITLE = Usb Wifi Exteriores Gsky 1000mw + Antena 12dbi  + 5mts Cable
ITEM COSTS $899.00

ELEMENT NUMBER 2
ITEM ID = 59860580
ITEM TITLE = Tarjeta De Red Usb 2 En 1 Wifi Y Bluetooth Con Antena 5dbi
ITEM COSTS $250.00
<?php // RAY_temp_degaray.php
error_reporting(E_ALL);
echo "<pre>";

// READ THE XML STRING INTO AN OBJECT
$url = "http://www.mercadolibre.com.mx/jm/searchXml?an_item_id=59860580&an_item_id=58774043";
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);
if (!$obj) die("BOGUS XML IN $url");

// ACTIVATE THIS TO SEE THE XML
// var_dump($obj);

$k = 0;
foreach ($obj->listing->items->item as $item)
{
    $k++;
    $i = (string)$item["id"];
    $t = (string)$item->title;
    $c = (string)$item->currency;
    $p = (string)$item->price;
    echo PHP_EOL;
    echo PHP_EOL . "ELEMENT NUMBER $k";
    echo PHP_EOL . "ITEM ID = $i";
    echo PHP_EOL . "ITEM TITLE = $t";
    echo PHP_EOL . "ITEM COSTS $c$p";
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:degaray
ID: 35177698
Thanks Ray_Paseur that was great!!!!!! I appreciate it very much.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35177746
Thanks for the points!  It's a really great question, ~Ray
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML XSL Message 3 14
test if query has no results 2 19
Scope of $_SESSION 17 30
Issue with XSLT mapping 10 21
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now