Php 使用DOMDocument创建复杂结构
我在使用PHP和Dom文档创建复杂的XML结构时遇到了一些问题 我希望结构如下所示:Php 使用DOMDocument创建复杂结构,php,xml,dom,xpath,Php,Xml,Dom,Xpath,我在使用PHP和Dom文档创建复杂的XML结构时遇到了一些问题 我希望结构如下所示: <page PathToWeb="www.mysite.com"> <Questions> <Question id="my id" member="true"> <Question id="my id2" member="true"> <Question id="my id3" member="tru
<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();
?>
load('myxml.xml');/*它只是一片空白*/
$xpath=newdomxpath($xml);
/*设置基本路径*/
$hrefs=$xpath->evaluate(“/page”);
/*将web路径添加到根/页*/
$href=$hrefs->item(0);
$href->setAttribute(“PathToWeb”,$PathToWeb);
/*使用Xpath创建复杂的XML*/
/*元素附加(将问题创建到/页面)*/
$href=$hrefs->item(0);
$element=$xml->createElement('Questions');
$href->appendChild($element);
/*XPATH求值*/
$hrefs=$xpath->evaluate(“/page/Questions”);
/*元素1追加*/
$href=$hrefs->item(0);
$element=$xml->createElement('Question');
$href->appendChild($element);
$hrefs=$xpath->evaluate(“/page/Questions/Question”);
$href=$hrefs->item(0);
$href->setAttribute(“id”、“我的id”);
/*元素2追加*/
$href=$hrefs->item(0);
$element=$xml->createElement('Question');
$href->appendChild($element);
$hrefs=$xpath->evaluate(“/page/Questions/Question”);
$href=$hrefs->item(0);
$href->setAttribute(“id”、“我的id”);
/*要素3追加*/
$href=$hrefs->item(0);
$element=$xml->createElement('Question');
$href->appendChild($element);
$hrefs=$xpath->evaluate(“/page/Questions/Question”);
$href=$hrefs->item(0);
$href->setAttribute(“id”、“我的id”);
$href=$hrefs->item(0);
$href->setAttribute(“成员”、“真”);
$string2=$xml->saveXML();
?>
创造的是:
<page PathToWeb="www.mysite.com">
<Questions><Question id="my id" member="true"><Question/></Question></Questions>
</page>
只编辑第一个问题
如何解决此问题?load('myxml.xml');/*它只是一片空白*/
<?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();
?>
$xpath=newdomxpath($xml);
/*设置基本路径*/
$base=$xpath->evaluate(“/page”)->项(0);
$base->setAttrubute(“PathToWeb”,$PathToWeb);
$questions=$xml->createElement('questions');
$base->appendChild($problems);
对于($i=0;$i<2;$i++){
$question=$xml->createElement('question');
$questions->appendChild($question);
$question->setAttribute(“id”、“我的id”);
$question->setAttribute(“成员”、“真”);
}
$string2=$xml->saveXML();
?>
这可能会帮助您解决问题,并使您的代码更加紧凑和易于处理:
返回新节点。然后您可以直接使用它。在附加子对象之后,无需使用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);
...
文档的根元素(
)也是如此。您不需要使用xpath来访问和操作它。它是:
/*创建DOM*/
$xml=新文档;
$xml->load('myxml.xml');/*它只是一片空白*/
/*将web路径添加到根/页*/
$href=$xml->documentElement;
$href->setAttribute(“PathToWeb”,$PathToWeb);
您的代码看起来比需要的复杂一些
由于appendChild
返回追加的节点,而setAttribute
返回set属性节点,因此您也可以通过链接方法调用和遍历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是DOMNodes的树层次结构是非常必要的。有关这方面的一些解释,请参见。$xml=newDOMDocument('1.0','UTF-8');
$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
$root=$xml->createElement('page');
$root->setAttribute(“PathToWeb”,$PathToWeb);
$wrap=$xml->createElement('Questions');
$root->appendChild($wrap);
对于($i=1;$icreateElement('question');
$element->setAttribute(“id”,“我的id”。$i);
$element->setAttribute(“成员”、“真”);
$wrap->appendChild($element);
}
$xml->appendChild($root);
$xml->formatOutput=true;
$xml->save('myxml.xml');//感谢Gordon
你如何解决具体问题?你从来没有接受过这里给出的任何答案。请你回顾并接受它们,或者指出它们没有解决你的问题的原因。谢谢。+1因为我从来没有想过在使用DOMDocument构建DOMTree时使用parentNode来实现链接。但是,我仍然认为循环是一个赌注如果你正在用数字ID构建元素,那该怎么办呢?想象一下会有二十个。@Liam绝对正确。问题元素太相似了,无法将它们添加到循环中。我只想强调如何遍历DOMTree。@Row Minds:如果这回答了你的问题,请随意接受它:-因此它被标记为已解决。
$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