PHP + XML 添加节点问题
大家好请帮帮忙.PHP 代码如下:
<?php
//Photo_XML类,继承PHP5的DomDocument类
class Photo_XML extends DOMDocument
{
private $Root; //属性
//构造函数
public function __construct()
{
parent::__construct();
if (!file_exists("siteContent.xml"))
{
$xmlstr = "<?xml version='1.0' encoding='GB2312'?><bookPages></bookPages>";
$this->loadXML($xmlstr);
$this->save("siteContent.xml");
}
else
{
$this->load("siteContent.xml");
}
}
//添加图片
public function addPhoto($address,$note)
{
$root = $this->documentElement;
//获取留言消息
$Node_address = $this->createElement("page"); //添加<page>标签
$type=$this->createAttribute("src");
$Node_address->appendChild($type); //添加src的属性
$type_text=$this->createTextNode($address);
$type->appendChild($type_text);
$text= $this->createTextNode(iconv("GB2312","UTF-8",$note));
$Node_address->appendChild($text);
//加入到根结点下
$root->appendChild($this->appendChild($Node_address));
$this->save("siteContent.xml");
}
}
?>
<?php
$photo_XML=new Photo_XML();
$photo_XML->addPhoto('./pages/p31.jpg','page31');
?>
XML效果如下:
问题:
效果往往出乎我的意外,我原本是想添加【<page src="./pages/p31.jpg">page31</page> 】在<bookPages></bookPages>这对标签内部的,但是~~~~~,所以我想问问高手们,知道如何改这个PHP代码???