xml が書籍に関するものか、著者に関するものかを決定する必要があります。
本に関するものであれば、次のようになります。
<?xml version="1.0"?>
<books>
<book>
<title>Beginning iOS 4 Application Development</title>
<author>WeiMengLee</author>
</book>
</books>
次に、次のように新しい本を追加できます。
$xml = new SimpleXMLElement("filename.xml", null, true);
$book = $xml->addChild('book');
$book->addChild('title', 'Another Book');
$book->addChild('Author', 'WeiMengLee');
$xml->asXML('filename.xml');
出力:-
<?xml version="1.0"?>
<books>
<book>
<title>Beginning iOS 4 Application Development</title>
<author>WeiMengLee</author>
</book>
<book>
<title>Another Book</title>
<Author>WeiMengLee</Author>
</book>
</books>
一方、xml が著者に関するものである場合は、次のようになります。
<?xml version="1.0"?>
<authors>
<author>
<name>WeiMengLee</name>
<books>
<book>Beginning iOS 4 Application Development</book>
</books>
</author>
</authors>
そして、次のような別の本を追加します:-
$xml = new SimpleXMLElement("filename.xml", null, true);
foreach($xml as $author){
if($author->name == 'WeiMengLee'){
$weimenglee = $author;
}
}
$weimenglee->books->addChild('book', 'Another book');
$xml->asXML('filename.xml');
出力:-
<?xml version="1.0"?>
<authors>
<author>
<name>WeiMengLee</name>
<books>
<book>Beginning iOS 4 Application Development</book>
<book>Another book</bmook>
</books>
</author>
</authors>