tags:

views:

41

answers:

4

Hi freinds, I am doing one task in that I have to get data from the link that i have. In that link the data is showing in xml format. How do I get the data using php

Thanks,

+1  A: 

You may want to try PHP's XMLReader.

fabrik
+1  A: 

Getting XML data is no different than any other. You can use file_get_contents as:

$xml = file_get_contents($url); 

This will get the entire XML in a string $xml which can later be parsed using say SimpleXML

codaddict
actualy there is url that giving the information on browser in xml format like <item id='1'><title>data 1</title><author>data 1</author></item> <item id='2'><title>data 2</title><author>data 2</author></item> I want this data lije title, author in array. So how this possible
Sachin
+1  A: 

Use SimpleXML, it's your best bet.

$xml = simplexml_load_file('test.xml');

You don't need to use file_get_contents first, as simplexml_load_file allows you to use full URLs and downloads the file for processing:

$xml = simplexml_load_file('http://www.example.com/test.xml');

$xml now contains an object of your XML file. It should be quite easy to work with, you can view the full structure by doing:

echo '<pre>'.print_r($xml, true).'</pre>';

It's important to think about your XML file's format. If it uses namespaces you'll have to do a little bit of tweaking to get the fields accessible, as these won't be available from the off.

mikemike
actualy there is url that giving the information on browser in xml format like <item id='1'><title>data 1</title><author>data 1</author></item> <item id='2'><title>data 2</title><author>data 2</author></item> I want this data lije title, author in array. So how this possible
Sachin
Off the top of my head, you'll have to convert the object into an array as I'm not sure SimpleXML can return an array for everything (some of it's elements are arrays), I doubt you'll be able to do it straight off. If you don't need it explicitly in an array then it should be dead simple:$array = array();$array['title'] = $xml->item[1]->title;$array['author'] = $xml->item[1]->author;This should work, but it's untested. You'll ahve to do a foreach on $xml->item if you want to loop through them all. As I said, use print_r to see the structure of the XML obj, then it's all very simple.
mikemike
A: 

You can use

Example:

$doc = new DOMDocument();
$doc->load('http://example.com/data.xml');
echo $doc->saveXML();

You have to have allow_url_fopen enabled in PHP.ini to load remote files. If not, you can see if cURL is enabled on your webserver and fetch the file into a variable. Once the XML string is in the variable, you can use use DOMDocument::loadXML to load the XML from a string.

To get elements from the data, you can use XPath queries or the other DOM API functions, e.g.

foreach( $doc->getElementsById('title') as $node) {
    // do something with the $node
}

Virtually any of the common usecases when working with DOM have been answered before on StackOverflow. You should have no problem finding them when using the search function. Or browse some of my previous answers.

Gordon