2011-07-16 12 views
2

PHPとDomドキュメントを使用して複雑な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

あなたのコードが必要以上にやや複雑に見えます。

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

DOMをDOMで操作する場合は、DOMがDOMNodeのツリー階層であることを理解することが不可欠です。その説明については、DOMDocument in phpを参照してください。

+0

+1 DOMDocumentを使用してDOMTreeを構築するときに、parentNodeを使用して連鎖を実現することは考えていませんでした。しかし、私はまだループがより良い方法になると思うあなたが数字のIDで要素を構築している場合は、20になると想像していた。 @Liamは絶対に –

+0

です。質問要素はあまりにも似ていてループに追加しない。私は本当に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 
関連する問題