php-使用XMLWriter生成XML文件

php-使用XMLWriter生成XML文件,php,xml,xmlwriter,Php,Xml,Xmlwriter,我正试图通过以下循环创建一个包含人员及其子女姓名的XML文件: $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('Pers

我正试图通过以下循环创建一个包含人员及其子女姓名的XML文件:

$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;
}
正如您所看到的,在输出文件中,我应该有多个名为“Person”的元素,这取决于列表中有多少人以及他们中有多少人有孩子

我希望最终的XML文档是这样的一个示例:

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

//乔有一个孩子叫威利
现在,我关心的是,我如何知道$xml_file->startElement('Person');选择了我刚刚创建的当前人员,而不是之前已创建的任何人员元素,因为它们的名称相同

如果最终XML文件的各个元素具有相同的名称,如何访问它们

最后,我想保存这个XML文档并将其内容打印到stdout


谢谢

startElement方法不会选择当前人员,而是启动另一个人。事实上,您可以使用writeElement或startElement添加元素,但不必同时使用这两种元素

请参见此示例:

<?php
    $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
    $xml->setIndent(true);

    //create the document tag
    $xml->startDocument();

    $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();
            }
        }
        $xml->endElement(); //End Personn

    }

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

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


?>

startElement方法不会选择当前人员,而是启动另一个人。事实上,您可以使用writeElement或startElement添加元素,但不必同时使用这两种元素

请参见此示例:

<?php
    $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
    $xml->setIndent(true);

    //create the document tag
    $xml->startDocument();

    $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();
            }
        }
        $xml->endElement(); //End Personn

    }

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

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


?>


谢谢你发这篇文章!我现在明白多了:)然而,我这里有一个问题。生成的子项不是以“Person”标记结尾,而是如下所示:即使在内部循环中调用了endElement函数。Il也应该是相同的。看:谢谢你的帖子!我现在明白多了:)然而,我这里有一个问题。生成的子项不是以“Person”标记结尾,而是如下所示:即使在内部循环中调用了endElement函数。Il也应该是相同的。见: