Solved

Extracting info from XML on PHP

Posted on 2011-03-20
5
446 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

708 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

19 Experts available now in Live!

Get 1:1 Help Now