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!
|
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!
|