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>
編集のみ最初の質問...
どうすればこの問題を解決できますか?これはあなたの問題を解決し、対処するためにあなたのコードはよりコンパクトかつ容易にするためにあなたを助けるかもしれない
どのように正確に解決できますか? – PeeHaa
あなたは決してここで答えを受け入れませんでした。彼らを見直して受け入れるか、彼らがあなたの問題を解決しなかった理由を指摘してください。ありがとう。 – Gordon