[php] php操作xml
xml文件
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <root> 3 <item id="Title" aa="ddd"><![CDATA[Discuz! Style]]></item> 4 <item id="Data" aa="ddd"> 5 <item id="name" aa="ddd"><![CDATA[默认风格]]></item> 6 <item id="templateid" aa="ddd"><![CDATA[1]]></item> 7 <item id="tplname" aa="ddd"><![CDATA[默认模板套系]]></item> 8 <item id="directory" aa="ddd"><![CDATA[./template/default]]></item> 9 <item id="copyright" aa="ddd"><![CDATA[康盛创想(北京)科技有限公司]]></item> 10 <item id="style" aa="ddd"> 11 <item id="smfont" aa="ddd"><![CDATA[Arial]]></item> 12 <item id="threadtitlefontsize" aa="ddd"><![CDATA[14px]]></item> 13 <item id="threadtitlefont" aa="ddd"><![CDATA[Tahoma,‘Microsoft Yahei‘,‘Simsun‘]]></item> 14 <item id="smfontsize" aa="ddd"><![CDATA[0.83em]]></item> 15 <item id="tabletext" aa="ddd"><![CDATA[#444]]></item> 16 <item id="midtext" aa="ddd"><![CDATA[#666]]></item> 17 <item id="lighttext" aa="ddd"><![CDATA[#999]]></item> 18 <item id="link" aa="ddd"><![CDATA[#333]]></item> 19 <item id="highlightlink" aa="ddd"><![CDATA[#369]]></item> 20 <item id="noticetext" aa="ddd"><![CDATA[#F26C4F]]></item> 21 <item id="bgcolor" aa="ddd"><![CDATA[#FFF background.png repeat-x 0 0]]></item> 22 <item id="msgfontsize" aa="ddd"><![CDATA[14px]]></item> 23 <item id="sidebgcolor" aa="ddd"><![CDATA[ vlineb.png repeat-y 0 0]]></item> 24 <item id="headerborder" aa="ddd"><![CDATA[0]]></item> 25 <item id="headerbgcolor" aa="ddd"><![CDATA[]]></item> 26 <item id="inputborderdarkcolor" aa="ddd"><![CDATA[#848484]]></item> 27 <item id="stypeid" aa="ddd"><![CDATA[1]]></item> 28 <item id="inputbg" aa="ddd"><![CDATA[#FFF]]></item> 29 <item id="commonborder" aa="ddd"><![CDATA[#CDCDCD]]></item> 30 <item id="commonbg" aa="ddd"><![CDATA[#F2F2F2]]></item> 31 <item id="specialborder" aa="ddd"><![CDATA[#C2D5E3]]></item> 32 <item id="specialbg" aa="ddd"><![CDATA[#E5EDF2]]></item> 33 <item id="dropmenuborder" aa="ddd"><![CDATA[#DDD]]></item> 34 <item id="floatmaskbgcolor" aa="ddd"><![CDATA[#000]]></item> 35 <item id="dropmenubgcolor" aa="ddd"><![CDATA[#FEFEFE]]></item> 36 <item id="floatbgcolor" aa="ddd"><![CDATA[#FFF]]></item> 37 <item id="lightlink" aa="ddd"><![CDATA[#FFF]]></item> 38 <item id="menuhoverbgcolor" aa="ddd"><![CDATA[#005AB4 nv_a.png no-repeat 50% -33px]]></item> 39 <item id="titlebgcolor" aa="ddd"><![CDATA[#E5EDF2 titlebg.png repeat-x 0 0]]></item> 40 <item id="fontsize" aa="ddd"><![CDATA[12px/1.5]]></item> 41 <item id="font" aa="ddd"><![CDATA[Tahoma,‘Microsoft Yahei‘,‘Simsun‘]]></item> 42 <item id="styleimgdir" aa="ddd"><![CDATA[]]></item> 43 <item id="imgdir" aa="ddd"><![CDATA[]]></item> 44 <item id="boardimg" aa="ddd"><![CDATA[logo.png]]></item> 45 <item id="available" aa="ddd"><![CDATA[]]></item> 46 <item id="headertext" aa="ddd"><![CDATA[#444]]></item> 47 <item id="footertext" aa="ddd"><![CDATA[#666]]></item> 48 <item id="menubgcolor" aa="ddd"><![CDATA[#2B7ACD nv.png no-repeat 0 0]]></item> 49 <item id="menutext" aa="ddd"><![CDATA[#FFF]]></item> 50 <item id="menuhovertext" aa="ddd"><![CDATA[#FFF]]></item> 51 <item id="wrapbg" aa="ddd"><![CDATA[#FFF]]></item> 52 <item id="wrapbordercolor" aa="ddd"><![CDATA[#CCC]]></item> 53 <item id="contentwidth" aa="ddd"><![CDATA[630px]]></item> 54 <item id="contentseparate" aa="ddd"><![CDATA[#C2D5E3]]></item> 55 <item id="inputborder" aa="ddd"><![CDATA[#E0E0E0]]></item> 56 </item> 57 <item id="version" aa="ddd"><![CDATA[X3.1]]></item> 58 </item> 59 </root>
1.DOMDocument方法
1 $xml=new DOMDocument(); 2 $xml->load($file); 3 //读出item节点 4 $item=$xml->getElementsByTagName("item");
2.节点的属性增删改操作
1 $xml=new DOMDocument(); 2 $xml->load($file); 3 $item=$xml->getElementsByTagName("item"); 4 foreach ($item as $key=>$v){ 5 echo $v->getAttribute("id");//获取属性为id的值 6 $v->setAttribute("aa",‘ddd‘);//增加一个新的属性aa 7 $v->removeAttribute("aa");//删除属性 8 9 }
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);//删掉第一个节点
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。