Solved

Extracting info from XML on PHP

Posted on 2011-03-20
5
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

Expert Comment

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

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 …

729 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