How to parse XML file with PHP

How to parse XML file with PHP

XML can be used on WebServices, API, storing data and many other cases. So in this tutorial I’ll show how to parse XML file with PHP. It’s very simple, just follow this tutorial.

Download source code

1. XML sample

This is an example of XML file.

<?xml version="1.0" encoding="UTF-8"?>
<result>
<title>How to parse JSON file with PHP</title>
<posts>
<post>
<title>How to parse JSON file with PHP</title>
<img>http://www.bewebdeveloper.com/images/uploads/how-to-parse-json-file-with-php.jpg</img>
<link>http://www.bewebdeveloper.com/tutorial-about-how-to-parse-json-file-with-php</link>
</post>
<post>
<title>How to Create an RSS Feed with PHP and MySQL</title>
<img>http://www.bewebdeveloper.com/images/uploads/how-to-create-an-rss-feed-with-php-and-mysql.jpg</img>
<link>http://www.bewebdeveloper.com/tutorial-about-how-to-create-an-rss-feed-with-php-and-mysql</link>
</post>
<post>
<title>Crop photo using PHP and jQuery</title>
<img>http://www.bewebdeveloper.com/images/uploads/crop-photo-using-php-and-jquery.jpg</img>
<link>http://www.bewebdeveloper.com/tutorial-about-crop-photo-using-php-and-jquery</link>
</post>
<post>
<title>Using Ajax with PHP/MySQL</title>
<img>http://www.bewebdeveloper.com/images/uploads/using-ajax-with-php-mysql.jpg</img>
<link>http://www.bewebdeveloper.com/tutorial-about-using-ajax-with-phpmysql</link>
</post>
</posts>
</result>

2. Basic parsing of XML file

Here is a basic example of parsing XML file. You change the "posts.xml" by the full url, ex: http://www.domain.tld/posts.php?format=xml

<?php
// load file content into an object
$xml = simplexml_load_file('posts.xml');
// read the title value
$title = $xml->title;
// copy the posts array into a php var
$list = $xml->posts->post;
// listing posts
foreach ($list as $item) {
echo $item->title;
}
?>

3. Files

index.php

This is the main file, it will be displayed on the first view

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to parse XML file with PHP</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="header">
<img src="images/BeWebDeveloper.png" />
</div><!-- header -->
<h1 class="main_title">How to parse XML file with PHP</h1>
<div class="content">
<a href="xml.php" class="button_xml">Display content from XML file</a>
</div><!-- content -->
<div class="footer">
Powered by <a href="http://www.bewebdeveloper.com">bewebdeveloper.com</a>
</div><!-- footer -->
</div><!-- container -->
</body>
</html>

xml.php

Here w’ll parse the XML file and display it’s content.

<?php
// load file content into an object
$xml = simplexml_load_file('posts.xml');
// read the title value
$title = $xml->title;
// copy the posts array into a php var
$list = $xml->posts->post;
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to parse JSON file with PHP</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="header">
<img src="images/BeWebDeveloper.png" />
</div><!-- header -->
<h1 class="main_title"><?php echo $title; ?></h1>
<div class="content">
<ul class="ul_xml clearfix">
<?php
foreach ($list as $item) {
?>
<li>
<a href="<?php echo $item->link; ?>">
<h2><?php echo $item->title; ?></h2>
<img src="<?php echo $item->img; ?>">
</a>
</li>
<?php
} // end foreach
?>
</ul>
</div><!-- content -->
<div class="footer">
Powered by <a href="http://www.bewebdeveloper.com">bewebdeveloper.com</a>
</div><!-- footer -->
</div><!-- container -->
</body>
</html>

style.css

This is the CSS file, it contain the design styles

* {
	margin: 0;
	padding: 0;
}
body {
	padding: 10px;
	background: #eaeaea;
	text-align: center;
	font-family: arial;
	font-size: 12px;
	color: #333333;
}
.container {
	width: 1000px;
	height: auto;
	background: #ffffff;
	border: 1px solid #cccccc;
	border-radius: 10px;
	margin: auto;
	text-align: left;
}
.header {
	padding: 10px;
}
.main_title {
	background: #cccccc;
	color: #ffffff;
	padding: 10px;
	font-size: 20px;
	line-height: 20px;
}
.content {
	padding: 10px;
	min-height: 100px;
	text-align: center;
}
.footer {
	padding: 10px;
	text-align: right;
}
.footer a {
	color: #999999;
	text-decoration: none;
}
.footer a:hover {
	text-decoration: underline;
}
.button_xml {
	width: 200px;
	height: 30px;
	display: block;
	text-align: center;
	background: #a3a3a3;
	border: 1px solid #808080;
	color: #ffffff;
	border-radius: 10px;
	cursor: pointer;
	margin: auto;
	margin-top: 20px;
	font-size: 14px;
	line-height: 30px;
	font-weight: bold;
	text-decoration: none;
}
.ul_xml {
	margin: auto;
	padding: 10px 0 10px 10px;
}
.ul_xml li {
	width: 462px;
	margin: 0 10px 10px 0;
	padding: 5px;
	list-style: none;
	text-align: left;
	background: #e1dfdf;
	border: 1px solid #cccccc;
	float: left;
}
.ul_xml li a {
	text-decoration: none;
	color: #666666;
}
.ul_xml li a h2 {
	font-size: 18px;
	margin: 5px 0 5px 0;
}
.ul_xml li a img {
	width: 100%;
	height: 138px;
}
.clearfix:after {
    clear: both;
    display: block;
    content: "";
}
PHP, read XML file, XML
comments powered by Disqus

Social Profiles

bewebdeveloper on facebook bewebdeveloper on twitter bewebdeveloper on google plus bewebdeveloper on vk bewebdeveloper on youtube bewebdeveloper on tumblr bewebdeveloper rss

Subscribe to our Newsletter

Facebook

Twitter

Google+

HTML Tutorials
CSS Tutorials

Advertising