Home » PHP » PHP Parser Example

About Olayemi Odunayo

Olayemi Odunayo
I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.

PHP Parser Example

In this example we are going to learn how to parse xml (Extensible Markup Language) with PHP. For this example we will use:

  1. A computer with PHP 5.5 installed
  2. notepad++

 
 
 
 
 
 

1. What is XML

XML(Extensible Markup Language) is a markup language like html. It is a syntax for creating data representation languages. XML was designed to store and transport data, It was also designed to be human and machine readable.
XML can be divided into two levels. On the first level XML defines a strict but simple syntax. When this syntax is followed, the document is a well formed XML document. On the second level XML provides a way of placing further restrictions on what can appear in a document. This is done by associating a DTD(Document Type Definition) with an XML document. A DTD is simply a list of things allowed to appear in the XML document.
If you are familiar with HTML, then you already have a basic idea of XML. An HTML document is not an XML document since it does not follow all the strict XML syntax rules.

parse.xml

<?xml version="1.0" ?>
<games>
<game>
<name>Shadow Clan  </name>
<version>1</version>
<multiplayer>true</multiplayer>
<size w="300" h="300"/>
<description>The Shadow Clan is a group of elite fighters. There mission is simple protect the world from Dark shadow clan </description>
<stage>2</stage>
</game>

<game>
<name>Zombie Escape  </name>
<version>3.2</version>
<multiplayer>false</multiplayer>
<size w="800" h="300"/>
<description>Lagos as been invaded by zombies, lead jane out of the danger zone </description>
<stage>2</stage>
</game>
<game>
<name>Zombie Escape 3 </name>
<version>1.0.2</version>
<multiplayer>false</multiplayer>
<size w="800" h="300"/>
<description>Lagos as been invaded by zombies, lead jane out of the danger zone </description>
<stage>2</stage>
</game>
</games>

This is a simple XML file that stores data about the games on a server. The first line identifies this document as an XML file. On this line we can also specify the character encoding used. The document is made up of elements, attributes and textual content. An element starts with a tag, such as <game> and ends with a matching </game> and the content is between the tags, which can consist of text and nested elements.
Unlike HTML the author of an XML document gets to choose the tags names and attribute names. Below are some rules for creating XML documents

  1. XML is case sensitive so <Title> is not the same as<title> .
    All XML elements must have closing tags.
  2. XML requires a root element (the <game> tag above serves as our root element)
  3. Attributes must be quoted
  4. Special characters (like &, <, and > signs) must be encoded.

1.1 Parsing XML from a string

To parse XML we are going to be using the SimpleXML parser. SimpleXML is a PHP extension that easily manipulates and get XML data. It turns an XML document into a data structure you can iterate through like a collection of arrays and objects.

fromstring.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	<title>Php Parser Example</title>
	</head>
	<body>
<?php
$par="<?xml version='1.0' ?>
<game>
<name>Shadow Clan</name>
<version>1</version>
<multiplayer>true</multiplayer>
<size w='300' h='300'/>
<description>The Shadow Clan is a group of elite fighters. There mission is simple protect the world from Dark shadow clan </description>
<stage>2</stage>
</game>";
$xml=simplexml_load_string($par);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} 
else{
print_r($xml);

}
?>
</body>
</html>

In line 22 we store our XML formated string in a variable named $par. In line 31 we call the simplexml_load_string($par); supplying our XML formatted string as an argument. In line 32 we check if our function returned successfully, if it did we use PHP print_r to print out our result. If an error occurred we print out the error.

1.2 Reading XML from a file

fromfile.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	<title>Php Parser Example</title>
	</head>
	<body>
<?php
$xml=simplexml_load_file("parse.xml")or die("An Error Occurred");
print_r($xml);
?>
</body>
</html>

In line 22 we call function simplexml_load_file("parse.xml") to parse our XML file supplying the name of the file to parse as an argument.

1.3 Getting Node Values

node.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	<title>Php Parser Example</title>
	</head>
	<body>
<?php
$par="<?xml version='1.0' ?>
<game>
<name>Shadow Clan</name>
<version>1</version>
<multiplayer>true</multiplayer>
<size w='300' h='300'/>
<description>The Shadow Clan is a group of elite fighters. There mission is simple protect the world from Dark shadow clan </description>
<stage>2</stage></game>";
$xml=simplexml_load_string($par) or die("Error Occurred");
echo "The name of the game is ". $xml->name."<br><br>";
echo "Game Description " . $xml->description. "<br><br>";
echo "The game version is ".$xml->version."<br><br>"
?>
</body>
</html>

Lets looks at another example.
node1.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	<title>Php Parser Example</title>
	</head>
	<body>
<?php
$par="<?xml version='1.0' ?>
<games>
<game>
<name>Shadow Clan</name>
<version>1</version>
<multiplayer>true</multiplayer>
<size w='300' h='300'/>
<description>The Shadow Clan is a group of elite fighters. There mission is simple protect the world from Dark shadow clan </description>
<stage>2</stage></game> <game>
<name>Zombie Escape  </name>
<version>3.2</version>
<multiplayer>false</multiplayer>
<size w='800' h='300'/>
<description>Lagos as been invaded by zombies, lead jane out of the danger zone </description>
<stage>2</stage>
</game>
<game>
<name>Zombie Escape 3 </name>
<version>1.0.2</version>
<multiplayer>false</multiplayer>
<size w='800' h='300'/>
<description>Lagos as been invaded by zombies, lead jane out of the danger zone </description>
<stage>2</stage>
</game></games>";
$xml=simplexml_load_string($par) or die("Error Occurred");
echo "The name of the game is ". $xml->game[1]->name."<br><br>";
echo "Game Description " . $xml->game[1]->description. "<br><br>";
echo "The game version is ".$xml->game[1]->version."<br><br>";

echo "The name of the game is ". $xml->game[2]->name."<br><br>";
echo "Game Description " . $xml->game[2]->description. "<br><br>";
echo "The game version is ".$xml->game[2]->version."<br><br>";
?>
</body>
</html>

1.4 Getting The Node Value With A Loop

node2.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	<title>Php Parser Example</title>
	</head>
	<body>
<?php
$xml=simplexml_load_file("parse.xml") or die("Error Occurred");
foreach($xml->children() as $game) {
echo "The name of the game is ". $game->name."<br><br>";
echo "Game Description " . $game->description. "<br><br>";
echo "The game version is ".$game->version."<br><br>";
}
?>
</body>
</html>

In this script we loop through the nodes(line 23 to 27)

2 Summary

In this example we have learnt about XML. We also learnt how to parse XML from a file and string in PHP.

3. Download the source code

Download
You can download the full source code of this example here: phpparserexample
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments