我敢肯定,这很容易,但像往常一样完全厚实,而且我是 SimpleXML 的新手。
我要做的就是根据给定的值添加和或编辑特定的注释。示例 xml 文件:
<site>
    <page>
     <pagename>index</pagename>
      <title>PHP: Behind the Parser</title>
      <id>abc
      <plot>
       So, this language. It's like, a programming language. Or is it a
       scripting language? All is revealed in this thrilling horror spoof
       of a documentary.
      </plot>
      </id>
      <id>def
      <plot>
      2345234 So, this language. It's like, a programming language. Or is it a
       scripting language? All is revealed in this thrilling horror spoof
       of a documentary.
      </plot>
      </id>
    </page>
     <page>
      <pagename>testit</pagename>
      <title>node2</title>
      <id>345
      <plot>
       345234 So, this language. It's like, a programming language. Or is it a
       scripting language? All is revealed in this thrilling horror spoof
       of a documentary.
      </plot>
      </id>
    </page>
    </site>
如果我想添加一个和索引如何找到节点键
我可以添加内容,例如
$itemsNode = $site->page->pagename;
$itemsNode->addChild("id", '12121')->addChild('plot', 'John Doe');
但我想要/需要做的是向 pagename='index' 或 pagename='testit' 添加内容某种形式的带有开关等的foreach循环。必须有一种简单的方法吗?不?
所以它看起来应该像我认为的那样(但不起作用,否则不会提出问题)
$paget = 'index' //(or testit')
if( (string) $site->page->pagename == $paget ){
$itemsNode = $site->page;
$itemsNode->addChild("id", '12121')->addChild('plot', 'John Doe');
}