Re: XML_Parser class
| From: | Tomas V.V.Cox | Date: | Wed, 04 Jul 2001 23:39:26 +0000 |
| Subject: | Re: XML_Parser class | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-552@lists.php.net to get a copy of this message | ||
yavor shahpasov wrote:
>
> Has anyone used the XML_Parser class. Can some one please shed some
> light on how to use it.
>
Here is the way I use it:
class MyParser extends XML_Parser
{
function MyParser () {
$this->folding = true;
// define parsing method
// 'func' method is not yet recommended
$this->XML_Parser(null, 'event');
}
function do ($file) {
// the file you want to parse
$err = $this->setInputFile($file);
if (PEAR::isError($err)) {
return $err;
}
// do the parse, at this moment the
// start, end and cdata funcs are called
// according the event (say what the parser
// finds running throw the xml file)
$err = $this->parse();
if (PEAR::isError($err)) {
return $err;
}
}
function StartHandler($xp, $elem, &$attribs) {
// this function is called every time
// a new element begins to be parsed
// ej: <name type="first">
// $elem='name'
// $attribs = array('type' => 'first')
}
function EndHandler($xp, $elem) {
// this function is called every time
// the current element ends
}
function cdataHandler($xp, $data) {
// this function is called every time
// character data outside tags are find
// ej: <name>My name is JJ</name>
// ^^^^^^^^^^^^^
}
}
$parser = new MyParser;
$err = $parser->do('myfile.xml');
if (PEAR::isError($err)){
die($err->getMessage());
}
You can see more working examples in pear/PEAR/Installer.php (the "do
work while parsing" approach) and in the doc publisher I sent you (the
"extract data first" approach). Look at the function xml_set_object() in
the PHP manual, is the main idea of this. The source code of the class
perhaps could be good resource also (yeah Luc, the doc ;)
Hope this can be understood (not very sure).
Tomas V.V.Cox