2011-07-16 183 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