Create and insert nodes into XML documents

巴扎黑
Release: 2016-11-23 10:36:48
Original
1173 people have browsed it

Php file:

"; echo "alert('您选择的位置【 ".$recom_position." 】已经存在,请返回重新选择');"; echo "window.location='index.html';"; echo ""; } else { $str=" ".$recom_position; $fp=fopen("data/config.ini","a"); fwrite($fp,$str); fclose($fp); //以xml格式组成字符串 $data="      "; //创建一个文档实例 $document = new DOMDocument('1.0', "UTF-8"); if(!is_file("data/AddArray.xml")) //判断是否存在此文件,如果不存在则执行下面代码,创建xml文档及结构 { //使用缩进格式化输出 $document->formatOutput = true; //创建根节点 $root = $document->createElement('priority'); $document->appendChild($root); //设置priority的属性 $root->setAttribute('elapsedTime','352'); $root->setAttribute('countNums','5'); //创建子节点 $datalist = $document->createElement('datalist'); //创建子节点 $data = $document->createElement('data'); //创建元素的第一个子节点,并设置其属性 $seqnum = $document->createElement('seqnum'); $seqnum->appendChild($document->createTextNode("")); $data->appendChild($seqnum); //创建元素的第二个子节点,并设置其属性 $recomposition = $document->createElement('recom_position'); $recomposition->appendChild($document->createTextNode("")); $data->appendChild($recomposition); //创建元素的第三个子节点,并设置其属性 $recomtitle = $document->createElement('recom_title'); $recomtitle->appendChild($document->createTextNode("")); $data->appendChild($recomtitle); //创建元素的第四个子节点,并设置其属性 $recomurl = $document->createElement('recom_url'); $recomurl->appendChild($document->createTextNode("")); $data->appendChild($recomurl); //创建元素的第五个子节点,并设置其属性 $recomcontent = $document->createElement('recom_content'); $recomcontent->appendChild($document->createTextNode("")); $data->appendChild($recomcontent); //追加节点到根节点后 $datalist->appendChild($data); $root->appendChild($datalist); //保存对象树到AddArray.xml $document->save('data/AddArray.xml'); if(is_file("data/AddArray.xml")) { echo ""; } else { echo "数据提交失败,点击这里返回重新提交"; } } else { //设置格式 $document->formatOutput = true; //加载文件 $document->load ('data/AddArray.xml'); //获取上级节点 $utdm = $document->getElementsByTagName ('datalist'); //data 节点 $info = $utdm->item(0); //获得data下面的子节点的数据 $seqnum = $document->createElement("seqnum",""); $recomposition = $document->createElement("recom_position",""); $recomtitle = $document->createElement("recom_title",""); $recomurl = $document->createElement("recom_url",""); $recomcontent = $document->createElement("recom_content",""); $data = $document->createElement("data"); //给每个节点添加内容 $data->appendChild($seqnum); $data->appendChild($recomposition); $data->appendChild($recomtitle); $data->appendChild($recomurl); $data->appendChild($recomcontent); $info->appendChild($data); //保存文档 $document->save("data/AddArray.xml"); $fp = fopen("data/AddArray.xml", "w"); //打开XML文件,写入 if(fwrite($fp,$document->saveXML())) { echo ""; } else { echo "数据提交失败,点击这里返回重新提交"; } fclose($fp);//关闭XML文件 } } } else { echo "没有获得任何数据"; } ?>
Copy after login


Related labels:
xml
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!