使用DOMDocument创建复杂的结构

Mas*_*345 2 php xml xpath dom

使用PHP和Dom Document创建复杂的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>
Run Code Online (Sandbox Code Playgroud)

我到目前为止的代码是

<?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();
?>  
Run Code Online (Sandbox Code Playgroud)

创造的是:

<page PathToWeb="www.mysite.com">
<Questions><Question id="my id" member="true"><Question/></Question></Questions>
</page>
Run Code Online (Sandbox Code Playgroud)

仅编辑第一个问题...

我怎么解决这个问题?

Gor*_*don 6

您的代码看起来比它需要的复杂一些.

因为appendChild返回附加节点并setAttribute返回设置的属性节点,所以您还可以创建整个树而不使用任何临时变量,也可以通过链接方法调用和遍历DOM树来创建任何Xpath:

$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();
Run Code Online (Sandbox Code Playgroud)

想要使用DOM时,了解DOM是DOMNode的树层次结构是必不可少的.请参阅php中的DOMDocument以获得一些解释.