Php SimpleXML的另一个注释

Php SimpleXML的另一个注释,php,xml,simplexml,Php,Xml,Simplexml,可能重复: 这是一个示例XML文件: <?xml version="1.0" encoding="ISO-8859-1"?> <notes> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me th

可能重复:

这是一个示例XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<notes>
   <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
   </note>
</notes>

托弗
贾尼
提醒
这个周末别忘了我!

如何使用
SimpleXML
添加另一个
note
(及其所有元素)?
据我所知,只向现有树添加一个子树。

好吧,首先我要得到一个更好的参考。可能是PHP手册,这是一个很好的资源:

这会将节点添加到
注释列表中:

<?php

$notesxml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<notes>
   <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
   </note>
</notes>
XML;

$notes = new SimpleXMLElement($notesxml);

$note = $notes->addChild('note');
$note->addChild('to', 'Yourself');
$note->addChild('from', 'Billy Brown');
$note->addChild('heading', 'Another note');
$note->addChild('body', 'This is the body');

echo $notes->asXML();

?>

托弗
贾尼
提醒
这个周末别忘了我!
XML;
$notes=新的simplexmlement($notesxml);
$note=$notes->addChild('note');
$note->addChild('to','Yourself');
$note->addChild('from','Billy Brown');
$note->addChild('heading','other note');
$note->addChild('body','This is the body');
echo$notes->asXML();
?>


你看的是对的。您只需先加载XML,不管它以何种形式出现。

谢谢,但我有一个现有文件,所以我需要先加载它,而不是创建一个新文件。你也有一个例子吗?哇,Jared花时间给你写了这么好的答案,而你只是不断地要求更多的代码@杰瑞德:为什么不删除答案?然后给我一个链接,指向你先前的一个答案,我可以悬赏吗?@user1170330-你有三个猜测,哪一个是正确的,但一个猜测就足够了。@hakre-Spoonfed For Life是新的瑜伽姿势。对一些人来说,这很容易。@JaredFarrish:耶,joga很酷。