Display XML Data with PHP?

Maxwell

Well-known member
Registered
Joined
Mar 5, 2013
Messages
189
Points
28
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

Well-known member
Registered
Joined
Dec 19, 2012
Messages
90
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

Well-known member
Registered
Joined
Mar 5, 2013
Messages
189
Points
28
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

Well-known member
Registered
Joined
Dec 19, 2012
Messages
90
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);
 
Latest Threads
Replies
0
Views
91
Replies
0
Views
57
Replies
0
Views
50
Replies
1
Views
81

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

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