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
8,232
Replies
10
Views
5,689
Replies
11
Views
7,450
Latest threads
Replies
0
Views
29
Replies
0
Views
33
Replies
1
Views
41
Replies
3
Views
106
Recommended threads
Replies
9
Views
3,848
Replies
0
Views
2,600
Replies
0
Views
2,945
Top