Author Topic: XML Parsing using PHP  (Read 4051 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
XML Parsing using PHP
« on: December 04, 2010, 12:04:47 AM »
If you are looking at this tutorial then you probably have a good idea in your head about what XML is and what you can do with it. However, for those of you that stumbled across this tutorial on accident and are interested in learning more, I will briefly explain what XML is and what it is used for. XML stands for eXtensible Markup Language and is used primarily for data storage and organization. It is useful for many things but the main thing I like about it is that there are no predefined tags; the author of the code completely creates the tags as he goes along. A sample xml code could look like this:
Code: [Select]
<?xml version="1.0"?>
As you can see the code is easy to read and understand. We can clearly see every tag and easily read them in plain English, or whatever language you are most comfortable with. Unfortunately this tutorial isn't about XML so if you wish to learn more about it then there are some good resources to be found all over the internet. Here are a couple of them: -XML FAQ [/SIZE]

Formatting XML

Ok, so you're probably asking, "Now what?" We have our XML code but when you view the XML page in the browser all you see is the XML code! That won't do! We need to find a way to format it. There are actually many ways to do this. I prefer to use PHP to get the job done. And that is what this tutorial is all about; using PHP to parse and format an XML document. If you don't know anything about PHP then you might not want to start here. A good place to start is They have a great beginner tutorial for dealing with the basics of PHP.
Now, before I continue I want to let everyone know that the following methods are simply the ways in which I prefer to go about getting the job done. They are in no way the only complete and comprehensive ways to do anything, ever, for any reason. If you think you have a better way then by all means, do it your way and if you like, please let me know about your way. With that said...
Creating our XML
As with any project, organization is the key. Well, since this is the easy level tutorial about XML parsing the actual XML I am going to use is super-duper simple:

Code: [Select]
<?xml version="1.0"?>
A simple list of numbers 1-10 with opening and closing tags for each number and then the higher level group tag "numbers" that contains all of the information in the XML document. That's really all for the XML. The majority of this tutorial is about PHP and introducing you to the functions that are necessary for formatting the XML.
Creating our PHP
To make this easy on us, I will post the code I used and then explain what each line does after.

$file = "xml_beginner.xml";

contents($parser, $data){

function startTag($parser, $data){

function endTag($parser, $data){

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($file, "r");

$data = fread($fp, 80000);

if(!(xml_parse($xml_parser, $data, feof($fp)))){
"Error on line " . xml_get_current_line_number($xml_parser));


Well again, you are probably asking, "Now what?" Well the only thing left to do is save your PHP file and run the script. Make sure that the PHP file and the XML file are in the same directory and that you have permission to read the XML file on the server. The output for this file isn't anything exciting, but if you did it correctly you should get this as your source:

Code: [Select]
    <b>1</b><br />
    <b>2</b><br />
    <b>3</b><br />
    <b>4</b><br />
    <b>5</b><br />
    <b>6</b><br />
    <b>7</b><br />
    <b>8</b><br />
    <b>9</b><br />
    <b>10</b><br />
</b><br />
That is pretty much it. There are a couple things to remember when working with XML.
1. Always free the parser memory 2. Always close the file 3. Always escape illegal XML characters a. < b. > c. & d. ' e. "
« Last Edit: December 04, 2010, 12:09:13 AM by admin »