Parsing XML from URL (simpleXml)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
basil_shine
Forum Newbie
Posts: 4
Joined: Fri Apr 17, 2009 4:33 am

Parsing XML from URL (simpleXml)

Post by basil_shine »

I have to read xml file into my page and parse it.
Im going this way:
$url = 'http://www.developments-online.com/live ... mlfeed.asp';
$xml = simplexml_load_file($url);

But this one gives me errors of missing document. So i dont know what to do. Was trying to use threads, but it didnt work either.
I should use simpleXml.

Thank you.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Parsing XML from URL (simpleXml)

Post by susrisha »

1. The given url is not an xml file. its probably calling or echoing a code whose output is an xml.
2. so do a curl operation with return var set and load the data.

Code: Select all

 
//Example URL:
$URL="http://www.developments-online.com/live/resales/export/xmlfeed.asp";
 
//initiate the curl 
$ch = curl_init();
//set the URL to the curl handle
curl_setopt($ch, CURLOPT_URL, $URL);
//ignore header
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
 
 
// grab URL and pass it to the browser
$returned_variable =curl_exec($ch);
 
// close CURL resource, and free up system resources
 
curl_close($ch);
 
$xml = simplexml_load_string($returned_variable ); 
 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Parsing XML from URL (simpleXml)

Post by requinix »

susrisha wrote:1. The given url is not an xml file. its probably calling or echoing a code whose output is an xml.
Dunno what you're talking about. When I go there I get XML data.

And the code works well for me.

Code: Select all

<?php
 
$url = 'http://www.developments-online.com/live/resales/export/xmlfeed.asp';
$xml = simplexml_load_file($url);
 
print_r($xml);

Code: Select all

SimpleXMLElement Object
(
    [resales_online] => SimpleXMLElement Object
        (
            [error_message] => Invalid Login Details - Access Denied
        )
 
)
Well, relatively well.
basil_shine
Forum Newbie
Posts: 4
Joined: Fri Apr 17, 2009 4:33 am

Re: Parsing XML from URL (simpleXml)

Post by basil_shine »

susrisha wrote:1. The given url is not an xml file. its probably calling or echoing a code whose output is an xml.
2. so do a curl operation with return var set and load the data.

Code: Select all

 
//Example URL:
$URL="http://www.developments-online.com/live/resales/export/xmlfeed.asp";
 
//initiate the curl 
$ch = curl_init();
//set the URL to the curl handle
curl_setopt($ch, CURLOPT_URL, $URL);
//ignore header
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
 
 
// grab URL and pass it to the browser
$returned_variable =curl_exec($ch);
 
// close CURL resource, and free up system resources
 
curl_close($ch);
 
$xml = simplexml_load_string($returned_variable ); 
 
 


This is great. Thank you very much. Couldn't find solution for this problem for a long time.
Now its working very well.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Parsing XML from URL (simpleXml)

Post by susrisha »

@tasairis
there might be a problem with allow_url_fopen which is restricting the opening of those files.
basil_shine
Forum Newbie
Posts: 4
Joined: Fri Apr 17, 2009 4:33 am

Re: Parsing XML from URL (simpleXml)

Post by basil_shine »

Rightn now i've tried to work with xml. But I'm having trouble picking the info out of the file. Could anyone please help?
It loads all xml right into the page. But i dont want it to load right in to it.
I just want to pick info.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Parsing XML from URL (simpleXml)

Post by php_east »

you already have the xml.

Code: Select all

$xml = simplexml_load_file($url);
you can do whatever you want with it. what specific help do you need ?
the DOM XML defines how and what are available to you.
User avatar
viraj
Forum Newbie
Posts: 11
Joined: Wed Nov 19, 2008 7:52 am
Location: Colombo - Sri Lanka

Re: Parsing XML from URL (simpleXml)

Post by viraj »

you can pick information from the xml file by using these functions.
http://www.php.net/manual/en/ref.simplexml.php
basil_shine
Forum Newbie
Posts: 4
Joined: Fri Apr 17, 2009 4:33 am

Re: Parsing XML from URL (simpleXml)

Post by basil_shine »

Thank you for your replies. As i told you before, everything worked when i was loading xml from local file. These way i can't use simplexml_load_string and simplexml_load_file because these code returns all xml file and execute it right in to the page ignoring simplexml_load_...
I mean i want to take that xml and work with it, but i dont want it to show up in the browser window, just some info from it.

Code: Select all

 
$xml = simplexml_load_string($returned_variable ); 
echo $xml->feed_version; // just as an example of idea that i want from $xml
 
It returns all xml file instead of only $value of $xml->
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Parsing XML from URL (simpleXml)

Post by php_east »

at the risk of myself sounding really stupid, if you don't want to put out the xml in your browser,
why then do you echo it out ? just work with the var.
Post Reply