I am trying to create an XML file containing the names of people and their children via the following cycle:
$xml_file = new XMLWriter();
$xml_file->startElement("People");
while ($list_of_people->current != NULL ){
$xml_file->writeElement("Person"); // create a new person
$xml_file->startElement('Person');
$xml_file->writeAttribute('name', $list_of_people->current->name); // add their name as an attribute
if ($list_of_people->current->children != NULL){
while ($list_of_people->current->child_current != NULL){ // if they have children create them as well
$xml_file->writeElement("Person");
$list_of_people->startElement('Person');
$xml_file->writeAttribute('name', $list_of_people->current->child_current->child_name);
$xml_file->endElement();
$list_of_people->current->child_current = $list_of_people->current->child_current->next;
}
}
$xml_file->endElement();
$list_of_people->current = $list_of_people->current->next;
}
As you can see, in the output file, I should have multiple elements that are named "Person" depending on how many people are in the list and how many of them have children.
An example of what I'd like the final XML document to look like would be something like this:
<People>
<Person name="Anna"></Person>
<Person name="Joe">
<Person name="Willy"></Person> // Joe has a child named Willy
</Person>
<Person name="Rob"></Person>
</People>
Now, my concern is this, how do I know that $xml_file->startElement('Person'); selected the current person I just created and not any of the Person elements that have already been created previously since they are all named the same?
And how to access individual elements of the final XML file if they have the same name?
Lastly, I would like to save this XML document and print out it's contents to stdout.
Thanks!