[php] php操作xml

xml文件

 <?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<item id="Title" aa="ddd"><![CDATA[Discuz! Style]]></item>
<item id="Data" aa="ddd">
<item id="name" aa="ddd"><![CDATA[默认风格]]></item>
<item id="templateid" aa="ddd"><![CDATA[]]></item>
<item id="tplname" aa="ddd"><![CDATA[默认模板套系]]></item>
<item id="directory" aa="ddd"><![CDATA[./template/default]]></item>
<item id="copyright" aa="ddd"><![CDATA[康盛创想(北京)科技有限公司]]></item>
<item id="style" aa="ddd">
<item id="smfont" aa="ddd"><![CDATA[Arial]]></item>
<item id="threadtitlefontsize" aa="ddd"><![CDATA[14px]]></item>
<item id="threadtitlefont" aa="ddd"><![CDATA[Tahoma,'Microsoft Yahei','Simsun']]></item>
<item id="smfontsize" aa="ddd"><![CDATA[0.83em]]></item>
<item id="tabletext" aa="ddd"><![CDATA[#444]]></item>
<item id="midtext" aa="ddd"><![CDATA[#666]]></item>
<item id="lighttext" aa="ddd"><![CDATA[#999]]></item>
<item id="link" aa="ddd"><![CDATA[#333]]></item>
<item id="highlightlink" aa="ddd"><![CDATA[#369]]></item>
<item id="noticetext" aa="ddd"><![CDATA[#F26C4F]]></item>
<item id="bgcolor" aa="ddd"><![CDATA[#FFF background.png repeat-x 0 0]]></item>
<item id="msgfontsize" aa="ddd"><![CDATA[14px]]></item>
<item id="sidebgcolor" aa="ddd"><![CDATA[ vlineb.png repeat-y 0 0]]></item>
<item id="headerborder" aa="ddd"><![CDATA[]]></item>
<item id="headerbgcolor" aa="ddd"><![CDATA[]]></item>
<item id="inputborderdarkcolor" aa="ddd"><![CDATA[#848484]]></item>
<item id="stypeid" aa="ddd"><![CDATA[]]></item>
<item id="inputbg" aa="ddd"><![CDATA[#FFF]]></item>
<item id="commonborder" aa="ddd"><![CDATA[#CDCDCD]]></item>
<item id="commonbg" aa="ddd"><![CDATA[#F2F2F2]]></item>
<item id="specialborder" aa="ddd"><![CDATA[#C2D5E3]]></item>
<item id="specialbg" aa="ddd"><![CDATA[#E5EDF2]]></item>
<item id="dropmenuborder" aa="ddd"><![CDATA[#DDD]]></item>
<item id="floatmaskbgcolor" aa="ddd"><![CDATA[#000]]></item>
<item id="dropmenubgcolor" aa="ddd"><![CDATA[#FEFEFE]]></item>
<item id="floatbgcolor" aa="ddd"><![CDATA[#FFF]]></item>
<item id="lightlink" aa="ddd"><![CDATA[#FFF]]></item>
<item id="menuhoverbgcolor" aa="ddd"><![CDATA[#005AB4 nv_a.png no-repeat 50% -33px]]></item>
<item id="titlebgcolor" aa="ddd"><![CDATA[#E5EDF2 titlebg.png repeat-x 0 0]]></item>
<item id="fontsize" aa="ddd"><![CDATA[12px/1.5]]></item>
<item id="font" aa="ddd"><![CDATA[Tahoma,'Microsoft Yahei','Simsun']]></item>
<item id="styleimgdir" aa="ddd"><![CDATA[]]></item>
<item id="imgdir" aa="ddd"><![CDATA[]]></item>
<item id="boardimg" aa="ddd"><![CDATA[logo.png]]></item>
<item id="available" aa="ddd"><![CDATA[]]></item>
<item id="headertext" aa="ddd"><![CDATA[#444]]></item>
<item id="footertext" aa="ddd"><![CDATA[#666]]></item>
<item id="menubgcolor" aa="ddd"><![CDATA[#2B7ACD nv.png no-repeat 0 0]]></item>
<item id="menutext" aa="ddd"><![CDATA[#FFF]]></item>
<item id="menuhovertext" aa="ddd"><![CDATA[#FFF]]></item>
<item id="wrapbg" aa="ddd"><![CDATA[#FFF]]></item>
<item id="wrapbordercolor" aa="ddd"><![CDATA[#CCC]]></item>
<item id="contentwidth" aa="ddd"><![CDATA[630px]]></item>
<item id="contentseparate" aa="ddd"><![CDATA[#C2D5E3]]></item>
<item id="inputborder" aa="ddd"><![CDATA[#E0E0E0]]></item>
</item>
<item id="version" aa="ddd"><![CDATA[X3.1]]></item>
</item>
</root>

1.DOMDocument方法

     $xml=new DOMDocument();
$xml->load($file);
//读出item节点
$item=$xml->getElementsByTagName("item");

2.节点的属性增删改操作

 $xml=new DOMDocument();
$xml->load($file);
$item=$xml->getElementsByTagName("item");
foreach ($item as $key=>$v){
echo $v->getAttribute("id");//获取属性为id的值
$v->setAttribute("aa",'ddd');//增加一个新的属性aa
$v->removeAttribute("aa");//删除属性 }

3.节点的增删改

    $xml=new DOMDocument();
$xml->load($file);
$node=$xml->documentElement;
// 添加节点
$root=$xml->getElementsByTagName('root')->item(0);//获得根节点
$elem=$xml->createElement("aa","dddd");//添加aa 节点
$elem_attr=$xml->createAttribute("test");//添加属性
$elem_attr->value="aaaa";//设置属性的值
$elem->appendChild($elem_attr);//添加到节点下
$root->appendChild($elem);//将aa 添加到根节点下
//删除节点
$node=$xml->documentElement;
$item=$xml->getElementsByTagName("item")->item(0);
$node->removeChild($item);//删掉第一个节点
上一篇:吴恩达课后作业学习2-week1-1 初始化


下一篇:多线程——Executor、ExecutorService、Executors三者的区别