#native_company# #native_desc#
#native_cta#

Converting XML into a PHP data structure Page 6

By PHP Builder Staff
on December 25, 2002

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.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|