高手求教,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 求高手帮帮忙! 展开
$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 求高手帮帮忙! 展开
展开全部
应该是这样:
$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");
$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");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询