I’m trying to parse a quite large XML-sheet with PHP, but I’m fairly new to it.
The XML-sheet contains a couple of thousands of records.
Here is an example of the structure used within the sheet:
<familyList>
<family>
<familyID>1234</familyID>
<familyDescription>The Jonathans</familyDescription>
<childrenList>
<child>Suzan</child>
<child>Fred</child>
<child>Harry</child>
</childrenList>
</family>
<family>
<familyID>1235</familyID>
<familyDescription>The Gregories</familyDescription>
<childrenList>
<child>Anthony</child>
<child>Lindsay</child>
</childrenList>
</family>
</familyList>
As I’m fairly new to XML-parsing using PHP, I wonder what would be the best way to parse this nested XML-sheet into an array. I need to convert the XML to an array so I can insert the data into a MySQL database afterwards.
Could you please give me a push in the right direction as I haven’t been succesful puzzling out a solution sofar?..
Thanks!
DOMDocument has lots of excellent methods for accessing, updating and outputting formatted XML. With regards to converting to an array, I’d suggest going for objects inside an array, which is something that PHP is perfectly fine with, and I find the syntax much clearer than arrays for keeping track of this kind of hierarchy.
Something like this, double check the syntax, but it’s on the way 😉