Display XML Data with PHP?

Maxwell

New member
Joined
Mar 5, 2013
Messages
397
Points
0
I have a xml file or xml url from other site and I want to extract data from this file/url and display it with php. Is it possible?
Can you give me how to do that?
thanks in advanced!
 

Alex_smith

New member
Joined
Dec 19, 2012
Messages
385
Points
0
Hello Maxwell
I will give you an example for you to understand which I'd like to explain :)
for example, you have a xml file ( mybook.xml ) with following information

Code:
<?xml version="1.0" encoding="utf-8"?>
<library name="mylibrary">
    <book>
        <title>webmaster forum</title>
        <author>maxwell</author>
        <category>affiliate marketing</category>
    </book>
    <book>
        <title>SEO forum</title>
        <author>alex</author>
        <category>search engine optimization</category>
    </book>
    <book>
        <title>internet marketing</title>
        <author>shirley</author>
        <category>ecommerce</category>
    </book>
    <book>
        <title>web hosting</title>
        <author>James</author>
        <category>special offers</category>
    </book>
</library>
to access data from this xml file, we can use extension SimpleXML of PHP. You will create a showresult.php in your web server and add these codes

Code:
<?php
$xml = simplexml_load_file("mybook.xml") or die("Unable to load XML file."); //load file xml
foreach($xml->book as $book){
    echo "Title: ".$book->title." - Author: ".$book->author." - Category: ".$book->category."<br />";
}
?>
Now, run showresult.php on your browser to see the results.

Good luck!
 

Maxwell

New member
Joined
Mar 5, 2013
Messages
397
Points
0
Cool @Alex
How to get xml data from a URL, I didn't see you mention how to do that in the codes?
 

Alex_smith

New member
Joined
Dec 19, 2012
Messages
385
Points
0
Alex_smith
You can use code simplexml_load_file(string $filename) to get data from a url. Just change the string $filename by the full URL such as:
Code:
$xmlurl="your full url";
$xmlinfo = simplexml_load_file($xmlurl);
print_r($xmlinfo);
 
Older threads
Replies
19
Views
7,901
Replies
10
Views
5,369
Replies
11
Views
7,099
Latest threads
Hi
Replies
1
Views
44
Replies
1
Views
43
Replies
1
Views
45
Replies
1
Views
45
Recommended threads

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top