Hi,
I'm attempting to write a PHP program to process some XML data. The data appears something like this:
<book>
<bookinfo>
<name>A book</name>
<publisher>Mr. Printer</publisher>
<authors>
<author>Mr. Writer</author>
<author>Mr. Editor</author>
</author>
</bookinfo>
<dbaseinfo>
<picture>www.mywebsite.com/picture.gif</picture>
<code>123</code>
</dbaseinfo>
</book>
The program I am currently using will populate the array $item[] as follows
$item['name'] = "A book";
$item['publisher'] = "Mr. Printer";
$item['author'] = "Mr. Writer";
... etc
What I should like it to do is reflect the structure of the XML file in the array. ie.
$item['bookinfo']['name'] = "A book";
$item['bookinfo']['authors'][0] = "Mr. Writer";
$item['bookinfo']['authors'][1] = "Mr. Editor";
... etc
My code, contained within a class, is setup so that, when open tags are encountered, the function
function tag_open($parser, $tag, $attributes)
{
$this->tag = $tag;
}
is always called, and the name of the tag can be stored to be used in the array. Character data is passed to
function cdata($parser, $cdata)
{
if ( ! isset($this->item[$this->tag]) ) {
$this->item[$this->tag] = $cdata;
}
}
which places the information sandwiched between the two XML tags into the array under the name of the tag. I have also included a
function tag_close($parser, $tag)
{
}
which does nothing at present.
Of course, what I should like this code to do now is sort the data out properly, paying attention to structure. Under the present system the multiple authors above will not be stored - only one author's name can be retained in $index[].
I am really quite new to PHP and not very sure how to code my way out of this problem. Could anyone suggest how I could modify my program to handle a multi-level XML file as simply as possible?
Cheers!
Incidentally, my previous post on this forum about putting strings into an array represents one idea I had to solving this problem. However, I suspect the idea I was playing with there is not very 'orthodox', and there is a better approach to this problem.