Archive:PHP and MySQL Programming/XML and PHP: Difference between revisions
wikademia>Ralfe No edit summary |
wikademia>Ralfe No edit summary |
||
| Line 30: | Line 30: | ||
?> | ?> | ||
You can think of the $parser variable in terms of a | You can think of the $parser variable in terms of a parsing engine for the XML document. Note that the ENCODING can be either : | ||
1. ISO-8859-1 (default) | 1. ISO-8859-1 (default) | ||
2. US-ASCII | |||
3. UTF-8 | |||
=== Defining the Element Handlers === | === Defining the Element Handlers === | ||
| Line 46: | Line 49: | ||
1. XML_PARSER - The variable that you created when you called the xml_parser_create() function. | 1. XML_PARSER - The variable that you created when you called the xml_parser_create() function. | ||
2. START_FUNCTION - The name of the function to call when the parser encounters a start element. | 2. START_FUNCTION - The name of the function to call when the parser encounters a start element. | ||
3. END_FUNCTION - The name of the function to call when the parser encounters an end element. | 3. END_FUNCTION - The name of the function to call when the parser encounters an end element. | ||
| Line 59: | Line 64: | ||
=== Defining Character Handlers === | === Defining Character Handlers === | ||
Character handlers are created by means of the set_character_handler() function as follows : | |||
<? | |||
xml_set_character_handler(XML_PARSER, CHARACTER_FUNCTION); | |||
?> | |||
The two arguments accepted by the set_character_handler() function are : | |||
1. XML_PARSER - The variable that you created when you called the xml_parser_create() function. | |||
2. CHARACTER_FUNCTION - The name of the function to call when the parser encounters character data. | |||
=== Starting the Parser === | === Starting the Parser === | ||
To finally start the parser, we call the xml_parse() function as follows : | |||
<? | |||
xml_parse(XML_PARSER, XML); | |||
?> | |||
The two arguments accepted by the xml_parse() function are : | |||
1. The variable that you created when you called the xml_parser_create() function. | |||
2. The XML that is to be parsed. | |||
e.g. : | |||
<? | |||
$f = fopen ("simple.xml", 'r'); | |||
$data = fread($f, filesize("simple.xml")); | |||
xml_parse($parser, $data); | |||
?> | |||
=== Cleaning Up === | === Cleaning Up === | ||
After parsing an XML document, it is considered good practise to free up the memory that is holding the parser. This is done by calling the xml_parser_free() function as follows : | |||
<? | |||
xml_parser_free(XML_PARSER); | |||
?> | |||
=== Example === | === Example === | ||
| Line 77: | Line 118: | ||
# ... some code | # ... some code | ||
} | } | ||
function characterData ($parser, $data){ | |||
# ... some code | |||
} | |||
function load_data($file){ | |||
$f = fopen ($file, 'r'); | |||
$data = fread($f, filesize($file)); | |||
return $data; | |||
} | |||
# --- Main Program Body --- | # --- Main Program Body --- | ||
$file = "simple.xml"; | |||
$parser = xml_parser_create(); | $parser = xml_parser_create(); | ||
xml_set_element_handler($parser, "startElement", "endElement"); | xml_set_element_handler($parser, "startElement", "endElement"); | ||
xml_set_character_data_handler($parser, "characterData"); | |||
xml_parse ($parser, load_data($file)); | |||
xml_parser_free($parser); | |||
?> | ?> | ||
Revision as of 10:02, 4 April 2006
Introduction
XML (eXtensible Markup Language) is used in mainstream development. It might have started off as an attempt at a web standard, but is now used even in more traditional applications as a document standard. For example, the Open Document Format employed by Sun in their StarOffice and OpenOffice suites is based on XML.
Because of its wide-spread use in the IT industry, it is fitting that we as PHP developers know how to make use of XML files in our PHP applications.
XML Structure
Since XML documents are extensible, there are no limits to the tags that you can create to define data with. Here is an example of a simple XML document :
<?xml version="1.0"?> <document> <title>Isn't this simple!</title> <body>XML is as simple as pie. :-)</body> </document>
The reason that it looks so simple, is because it is so simple! Just as in HTML, elements are enclosed by angled brackets : "<" and ">", where the start element differs from the end element by the exclusion of a forward slash : "/".
Creating an XML Parser in PHP
Defining the XML Parser
In PHP, you define an XML Parser by using the xml_parser_create() function as shown below.
<? $parser = xml_parser_create(ENCODING); ?>
You can think of the $parser variable in terms of a parsing engine for the XML document. Note that the ENCODING can be either :
1. ISO-8859-1 (default)
2. US-ASCII
3. UTF-8
Defining the Element Handlers
Element handlers are defined by means of the xml_set_element_handler() function as follows :
<? xml_set_element_handler(XML_PARSER, START_FUNCTION, END_FUNCTION); ?>
The three arguments accepted by the xml_set_element_handler() function are :
1. XML_PARSER - The variable that you created when you called the xml_parser_create() function.
2. START_FUNCTION - The name of the function to call when the parser encounters a start element.
3. END_FUNCTION - The name of the function to call when the parser encounters an end element.
e.g. :
<? $parser = xml_parser_create(); xml_set_element_handler($parser, "startElement", "endElement"); ?>
Defining Character Handlers
Character handlers are created by means of the set_character_handler() function as follows :
<? xml_set_character_handler(XML_PARSER, CHARACTER_FUNCTION); ?>
The two arguments accepted by the set_character_handler() function are :
1. XML_PARSER - The variable that you created when you called the xml_parser_create() function.
2. CHARACTER_FUNCTION - The name of the function to call when the parser encounters character data.
Starting the Parser
To finally start the parser, we call the xml_parse() function as follows :
<? xml_parse(XML_PARSER, XML); ?>
The two arguments accepted by the xml_parse() function are :
1. The variable that you created when you called the xml_parser_create() function.
2. The XML that is to be parsed.
e.g. :
<?
$f = fopen ("simple.xml", 'r');
$data = fread($f, filesize("simple.xml"));
xml_parse($parser, $data);
?>
Cleaning Up
After parsing an XML document, it is considered good practise to free up the memory that is holding the parser. This is done by calling the xml_parser_free() function as follows :
<? xml_parser_free(XML_PARSER); ?>
Example
<?
# --- Element Functions ---
function startElement($parser, $name, $attributes){
# ... some code
}
function endElement ($parser, $name){
# ... some code
}
function characterData ($parser, $data){
# ... some code
}
function load_data($file){
$f = fopen ($file, 'r');
$data = fread($f, filesize($file));
return $data;
}
# --- Main Program Body ---
$file = "simple.xml";
$parser = xml_parser_create();
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");
xml_parse ($parser, load_data($file));
xml_parser_free($parser);
?>