Daeto Daeto -4 years ago 114
PHP Question

php - generating an XML file with XMLWriter

I am trying to create an XML file containing the names of people and their children via the following cycle:

$xml_file = new XMLWriter();


while ($list_of_people->current != NULL ){

$xml_file->writeElement("Person"); // create a new 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->writeAttribute('name', $list_of_people->current->child_current->child_name);


$list_of_people->current->child_current = $list_of_people->current->child_current->next;


$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:

<Person name="Anna"></Person>
<Person name="Joe">
<Person name="Willy"></Person> // Joe has a child named Willy
<Person name="Rob"></Person>

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.


Answer Source

The startElement method doesn't select the current person, it starts another. In fact you can add element with writeElement or startElement but without using both.

See this example:

    $people = array(
        array('name' => 'Anne', 'children' => array()),
        array('name' => 'Joe', 'children' => array(array('name' => 'Willy')))

    //create a new xmlwriter object
    $xml = new XMLWriter(); 

    // Used to write to xml file - uncomment the next line to print xml to a file
    // $xml->openURI('people_test.xml');

    // used for string output from memory
    $xml->openMemory(); // comment this line to print xml to a file

    //set the indentation to true

    //create the document tag

    $xml->startElement("People"); // start People

    foreach($people as $person) {

        $xml->startElement("Person"); // start Person
        $xml->writeAttribute('name', $person['name']);

        if(!empty($person['children'])) {
            foreach($person['children'] as $child) {
                $xml->startElement("Person"); // start Person
                $xml->writeAttribute('name', $child['name']);
        $xml->endElement(); //End Personn


    $xml->endElement(); //End People

    // Display thue current buffer
    echo $xml->flush(); 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download