用DOMDocument创建一个复杂的结构


Create a complex structure with DOMDocument

我在使用PHP和Dom文档创建复杂的XML结构时遇到了一个小问题。

我想让结构像这样:

<page PathToWeb="www.mysite.com">
    <Questions>
        <Question id="my id" member="true">
        <Question id="my id2" member="true">
        <Question id="my id3" member="true">
    </Questions>
</page>

目前我的代码是

<?php
/*Create DOM*/
$xml = new DOMDocument;
$xml->load('myxml.xml'); /* wich is just just blank <?xml?'> <page> </page>*/
$xpath = new DOMXPath($xml);
/*Set the base path*/
$hrefs = $xpath->evaluate("/page");
/*Add Path to web to the root /page*/
$href = $hrefs->item(0);
$href->setAttribute("PathToWeb",$PathToWeb);

/*Complex XML Creation with Xpath*/
/*ELEMENT APPEND (create questions into /page)*/
$href = $hrefs->item(0);
$element = $xml->createElement('Questions');
$href->appendChild($element);
/*XPATH EVALUATE*/
$hrefs = $xpath->evaluate("/page/Questions");
/*ELEMENT 1 APPEND*/
$href = $hrefs->item(0);
$element = $xml->createElement('Question');
$href->appendChild($element);
$hrefs = $xpath->evaluate("/page/Questions/Question");
$href = $hrefs->item(0);
$href->setAttribute("id","my id");
/*ELEMENT 2 APPEND*/
$href = $hrefs->item(0);
$element = $xml->createElement('Question');
$href->appendChild($element);
$hrefs = $xpath->evaluate("/page/Questions/Question");
$href = $hrefs->item(0);
$href->setAttribute("id","my id");
/*ELEMENT 3 APPEND*/
$href = $hrefs->item(0);
$element = $xml->createElement('Question');
$href->appendChild($element);
$hrefs = $xpath->evaluate("/page/Questions/Question");
$href = $hrefs->item(0);
$href->setAttribute("id","my id");
$href = $hrefs->item(0);
$href->setAttribute("member","true");
$string2 = $xml->saveXML();
?>  

创建的是:

<page PathToWeb="www.mysite.com">
<Questions><Question id="my id" member="true"><Question/></Question></Questions>
</page>

只编辑第一个问题…

我该如何解决这个问题?

你的代码看起来比实际需要的要复杂一些。

因为appendChild返回附加的节点,而setAttribute返回set Attribute node,您也可以创建没有任何临时变量的整个树,也不需要任何Xpath,只需通过链接方法调用和遍历DOM树:

$dom = new DOMDocument('1.0', 'utf-8');
$dom->appendChild($dom->createElement('page'))
    ->setAttribute('PathToWeb', 'www.mysite.com')
        ->parentNode
    ->appendChild($dom->createElement('Questions'))
        ->appendChild($dom->createElement('Question'))
            ->setAttribute('id', 'my_id')
                ->parentNode
            ->setAttribute('member', 'true')
                ->parentNode
            ->parentNode
        ->appendChild($dom->createElement('Question'))
            ->setAttribute('id', 'my_id2')
                ->parentNode
            ->setAttribute('member', 'true')
                ->parentNode
            ->parentNode
    ->appendChild($dom->createElement('Question'))
            ->setAttribute('id', 'my_id3')
                ->parentNode
            ->setAttribute('member', 'true');
$dom->formatOutput = true;
echo $dom->saveXml();

当想要使用DOM时,理解DOM是domnode的树状层次结构是必不可少的。

    $xml = new DOMDocument('1.0','UTF-8');
    $root = $xml->createElement('page');
    $root->setAttribute("PathToWeb",$PathToWeb);
    $wrap = $xml->createElement('Questions');
    $root->appendChild($wrap);
    for ($i = 1;$i<4;$i++)
    {
    $element = $xml->createElement('question');
    $element->setAttribute("id","my id" . $i);
    $element->setAttribute("member","true");
    $wrap->appendChild($element);
    }
    $xml->appendChild($root);
    $xml->formatOutput = true;
    $xml->save('myxml.xml');// Thanks to Gordon
<?php
$xml = new DOMDocument;
$xml->load('myxml.xml'); /* wich is just just blank <?xml?> <page> </page>*/
$xpath = new DOMXPath($xml);
/*Set the base path*/
$base = $xpath->evaluate("/page")->item(0);
$base->setAttrubute("PathToWeb", $PathToWeb);
$questions = $xml->createElement('Questions');
$base->appendChild($questions);
for($i = 0; $i < 2; $i++) {
    $question= $xml->createElement('Question');
    $questions->appendChild($question);
    $question->setAttribute("id","my id");
    $question->setAttribute("member", "true");
}
$string2 = $xml->saveXML();
?>  

这可能会帮助您解决问题,并使您的代码更紧凑,更容易处理:

appendChild PHP Manual返回新节点。然后您可以直接使用它。不需要在附加子元素后使用xpath来访问它。

如果你在添加元素节点到文档之前添加/设置你想要设置的属性,你通常甚至不需要:

/*ELEMENT APPEND (create questions into /page)*/
$href = $hrefs->item(0);
$element = $xml->createElement('Questions');
$questions = $href->appendChild($element);
#   ^^^

/*ELEMENT 1 APPEND*/
$element = $xml->createElement('Question');
$element->setAttribute("id","my id"); # prepare before adding
$questions->appendChild($element);
...

对于文档的根元素(<page>)也是一样的。您不需要使用xpath来访问和操作它。是documentElement <一口> PHP手册:

/*Create DOM*/
$xml = new DOMDocument;
$xml->load('myxml.xml'); /* wich is just just blank <?xml?> <page> </page>*/
/*Add Path to web to the root /page*/
$href = $xml->documentElement;
$href->setAttribute("PathToWeb",$PathToWeb);