高手求教,PHP怎么向xml中插入新信息?

之前生成了一个XML文件$doc=newDOMDocument('1.0','utf-8');$doc->formatOutput=true;$folder=$doc->... 之前生成了一个XML文件

$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
$folder = $doc -> createElement('folder');
$file = $doc -> createElement('file');

$pname = $doc -> createAttribute('pname');
$pclass = $doc -> createAttribute('class');
$pcar = $doc -> createAttribute('pcar');
$newspname = $doc -> createTextNode($name);
$newspclass = $doc -> createTextNode($_POST['Sclass']);
$newspcar = $doc -> createTextNode($_POST['Scar']);

$pname -> appendChild($newspname);
$pclass -> appendChild($newspclass);
$pcar -> appendChild($newspcar);

$file -> appendChild($pname);
$file -> appendChild($pclass);
$file -> appendChild($pcar);
$doc ->appendChild($folder);

$folder -> appendChild($file);
$doc -> save('ico/Thumb/'."pic.xml");
XML文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<folder>
<file pname="1336987243222652.jpg" class="1" pcar="测试"/>
</folder>
我想加一条<file pname="133698724000000.jpg" class="000" pcar="测试2"/>
我添加的语句是:

$doc = new DOMDocument();
$doc->load( 'ico/Thumb/'."pic.xml");

$folder = $doc->getElementsByTagName("folder");
$new_file=$doc->createElement("file");

$pname = $doc -> createAttribute('pname');
$pclass = $doc -> createAttribute('class');
$pcar = $doc -> createAttribute('pcar');

$newspname = $doc -> createTextNode($name);
$newspclass = $doc -> createTextNode($_POST['Sclass']);
$newspcar = $doc -> createTextNode($_POST['Scar']);

$pname -> appendChild($newspname);
$pclass -> appendChild($newspclass);
$pcar -> appendChild($newspcar);

$new_file -> appendChild($pname);
$new_file -> appendChild($pclass);
$new_file -> appendChild($pcar);

$folder -> appendChild($new_file);
$doc ->appendChild($folder);

$doc -> save('ico/Thumb/'."pic.xml");
然后就报错Call to undefined method DOMNodeList::appendChild() in C:\ProgramFiles\wamp\www\GraduateAlbumQ\user\up.php on line 73 求高手帮帮忙!
展开
 我来答
ys5955092
2012-05-25 · TA获得超过671个赞
知道小有建树答主
回答量:551
采纳率:100%
帮助的人:571万
展开全部
应该是这样:
$doc = new DOMDocument();
$doc->load( "pic.xml");
//这里你要获取他的第一个tag,因为getElementsByTagName获取的结果是个数组对象
$folder = $doc->getElementsByTagName("folder") -> item(0);

$new_file=$doc->createElement("file");

$pname = $doc -> createAttribute('pname');
$pclass = $doc -> createAttribute('class');
$pcar = $doc -> createAttribute('pcar');

$newspname = $doc -> createTextNode($name);
$newspclass = $doc -> createTextNode($_POST['Sclass']);
$newspcar = $doc -> createTextNode($_POST['Scar']);

$pname -> appendChild($newspname);
$pclass -> appendChild($newspclass);
$pcar -> appendChild($newspcar);

$new_file -> appendChild($pname);
$new_file -> appendChild($pclass);
$new_file -> appendChild($pcar);

$folder -> appendChild($new_file);
//$doc ->appendChild($folder);//前面已经append了,就不需要在append了

$doc -> save("pic.xml");
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式