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:
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
and you’ll have to concat the strings together until the end tag is closed.
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 toand you’ll have to concat the strings together until the end tag is closed.