Hudzilla.org - the homepage of Paul Hudson
Contents > XML & XSLT > Event-based parsing Wish List | Report Bug | About Me ]

12.2.1     Creating a parser: xml_parser_create() and xml_parser_free()

This is NOT the latest copy of this book; click here for the latest version.

resource xml_parser_create ( [string encoding])

bool xml_parser_free ( resource parser)

The first step in parsing an XML document is to create an instance of the PHP XML parser. PHP has two specific functions for the very purpose of managing instances of XML parsers: xml_parser_create() and xml_parser_free(). xml_parser_create() takes no parameters and returns a reference to an XML parser which is used in other XML functions; you should keep this reference stored in a variable for later use.

xml_parser_free() takes the parser instance you got back from xml_parser_create() and destroys it, freeing up the memory the parser was using up. PHP will, of course, clean up for you when your script ends, but it is always good house-keeping practice to clean up unused resources.

Here is an example of creating and freeing a parser, just so you are clear on how straightforward it is:

$parser = xml_parser_create();
// ... complicated XML stuff here ...
xml_parser_free($parser);

Naturally, you will need something to fill in the "complicated XML stuff here" part in between creating a parser and freeing it. Read on!





<< 12.2 Event-based parsing   12.2.2 Getting to know callback functions >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow