Converting XML Into a PHP Data Structure - Watching the XML Parsing Events: Callback Functions
(Page 5 of 9 )
What do we expect to happen when the above code is executed? Well, each time the xml_parse function encounters an XML tag in our document it'll fire an event by calling the functions we told it to call.
The term for this behavior is often referred to as a 'Callback Function'. Basically we want PHP to call us back at a given function name each time it triggers an event of a certain type.
By using the function, xml_set_element_handler, we are letting the PHP parser know that the open tag should invoke a method in our class named 'startElement' and all close tags should invoke a method in our class named 'endElement':
xml_set_element_handler($this->parser, "startElement", "endElement");
Additionally, we want to capture all the character data between tags, so we use the method, xml_set_character_data_handler to define the callback function as 'characterData':
xml_set_character_data_handler($this->parser, "characterData");
Callback functions are a very powerful tool that many languages offer and they work great in this specific case. Until I write an article on using callback functions, just accept that it 'simply works', and let's see what events are fired as we parse our sample XML document:
START: [drive]
DATA: []
DATA: [ ]
START: [folder]
DATA: []
DATA: [ ]
START: [file]
END: [file]
DATA: []
DATA: [ ]
START: [file]
END: [file]
DATA: []
DATA: [ ]
END: [folder]
DATA: []
DATA: [ ]
START: [folder]
DATA: []
DATA: [ ]
START: [file]
END: [file]
DATA: []
DATA: [ ]
START: [file]
DATA: []
DATA: [ This is a comment about file d.]
DATA: []
DATA: [ We like comments.]
END: [file]
DATA: []
DATA: [ ]
END: [folder]
DATA: []
END: [drive]
Did you expect to see that? Notice that each time a tag is opened, we see the 'START: [tagname]' line printed. When a tag is closed, we see the 'END: [tagname]' lines. Finally, whenever data is encountered, we get the 'DATA: [...]' lines.
Notice, though that the data lines are not necessarily together. Rules in parsing say that you can not guarantee that the data will always be together in one chunk. In fact, it's likely that it will NOT be together.
The PHP parser is allowed to call your characterData callback method as many times as it needs to and you'll have to concat the strings together until the end tag is closed.
Next: Building the Array Tree >>
More PHP Articles
More By Dante Lorenso