2011-07-16 88 views
2

使用PHP和Dom Document创建复杂的XML结构时遇到了一些问题。使用DOMDocument创建复杂的结构

我想要的结构是这样的:

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

编辑只第一个问题...

我该如何解决这个问题?

+0

你如何解决究竟是什么? – PeeHaa

+0

你从未接受过这里给出的任何答案。你可以请审查并接受他们或指出为什么他们没有解决你的问题。谢谢。 – Gordon

回答

0
<?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(); 
?> 
0

这可以帮助你解决你的问题,使你的代码更紧凑,更容易处理:

appendChildPHP 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来访问并操作它。这是documentElementPHP Manual

/*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); 
+0

男人,这就是我要找的,非常感谢你! – Master345

+0

@Row Minds:如果回答你的问题,请随时接受它:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work - 所以它被标记为已解决。 – hakre

5

您的代码看起来有点复杂得多,它需要的。

$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(); 

因为appendChild返回附加节点和setAttribute返回集合属性节点,你也可以简单地通过链接的方法调用,并遍历DOM树创建整个树没有任何临时变量,也没有任何XPath

当想要使用DOM时,了解DOM是DOMNodes的树层次结构是非常重要的。关于这方面的一些解释见DOMDocument in php

+0

+1,因为我从来没有想过在使用DOMDocument构建DOMTree时使用parentNode来实现链接。不过,我仍然认为如果你用数字ID构建元素,循环会是更好的方式,想象会有二十个。 –

+0

@Liam绝对。问题元素太相似,不能将它们添加到循环中。我真的只想强调如何遍历DOMTree。 – Gordon

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