PHP Modify XML node attributes, add XML node attributes of the code, the need for friends can refer to the next
PHP modifies the code that adds XML node attributes for your reference.
PHP modifies XML node attributes, adds XML node attributes to the code, has the need for friends, refer to below.
1. xml file
Copy the code code as follows:
<?xml version="1.0"encoding="UTF-8"? ><clientset><server url="192.168.0.180"port="1935"/><rootpath value=""/>"http://www.jbxue.com"/>"help.html"/><language value="en"/><theme value="default"/><visiblemarquee value ="true"/><visiblewhitepaper value="true"/><showmemberroomforguest value ="true"/><emotions enabled="true"column="5"autoplay="false"><item name="Birthday"Src="cartoon/movie/birthday.swf"thumb="cartoon/preview/birthday-small.swf"duration=" the"/><item name="Boom"Src="cartoon/movie/boom.swf"thumb="cartoon/preview/boom-small.swf"duration="6"/><item name="Bubble"Src="cartoon/movie/bubble.swf"thumb="cartoon/preview/bubble-small.swf"duration="7.5"/><item name="Cry"Src="cartoon/movie/cry.swf"thumb="cartoon/preview/cry-small.swf"duration="5.4"/><item name="Doggie"Src="cartoon/movie/doggie.swf"thumb="cartoon/preview/doggie-small.swf"duration=" -"/><item name="Greeting"Src="cartoon/movie/greeting.swf"thumb="cartoon/preview/greeting-small.swf"duration="7.4"/><item name="Football"Src="cartoon/movie/football.swf"thumb="cartoon/preview/football-small.swf"duration="2.2"/></emotions ></clientSet>
2. PHP code
< $dom =new DOMDocument (' 1.0 '); $dom->load (' x.xml '); $em = $dom->getelementsbytagname (' emotions '); $em = $em- >item (0); $items = $em->getelementsbytagname (' item '), foreach ($items as $a) {foreach ($a->attributes as $b) {if ( $b->nodevalue== ' Birthday ') {$a->setattribute (' name ', ' Nbirthday ');}}} $t = $dom->createelement (' item '); $t->setattribute (' name ', ' X '); $t->setattribute (' src ', ' www.baidu.com '); $ T->setattribute (' duration ', ' duration '), $em->appendchild ($t); $dom->save (' x.xml ');? >
PHP parses XML document properties and edits
<?PHP//Read XML$dom =NewDOMDocument ('1.0'); $dom->load ('Data.xml'); $em= $dom->getelementsbytagname ('Videos');//Outermost node$em = $em->item (0); $items= $em->getelementsbytagname ('Video');//node//If you do not need to read directly add the following paragraph can be removedforeach($items as$a) { foreach($a->attributes as$b) {//$b The value of the->nodevalue; node property $b->nodename; The name of the node propertyEcho $bNodeName; Echo":";//www.jbxue.comEcho $bNodeValue; Echo"<br/>"; } } //The following is a new write to the XML line$t = $dom->createelement ('Video');//<video$t->setattribute ('title','1');//<video name= "Data"$t->setattribute ('src','2');//<video name= "Data" src= "2"$t->setattribute ('img','1');//<video name= "Data" img= "1"$em->appendchild ($t);//<video name= "Data" img= "1"/>$dom->save ('Data.xml'); ?>
XML document at the time:
<?xml version= "1.0"?> <videos> <video img= "A" url= "1" title= "1" nickname= "1" tag= "1" vid= "1" star= "1 "/> <video img=" B "url=" 2 "title=" 2 "nickname=" 2 "tag=" 2 "vid=" 2 "star=" 2 "/> <video img=" C "url=" 3 " Title= "3" nickname= "3" tag= "3" vid= "3" star= "3"/>
Modified to modify the XML
<?PHP $doc=NewDOMDocument (); $doc->load ('Data.xml'); //Find Videos Nodes$root = $doc->getelementsbytagname ('Videos'); //First Videos Node$root = $root->item (0); //find the video node under the videos node$userid = $root->getelementsbytagname ('Video'); //traverse all video nodesforeach($userid as$rootdata) { //traverse all properties of each video nodeforeach($rootdata->attributes as$attrib) {$attribName= $attrib->nodename;//NodeName is the property name$attribValue = $attrib->nodevalue;//NodeValue as attribute content//Find node content with property name IPif($attribName = ='img') { //find the content of the node with the property content as IPif($attribValue = ='1') { //Modify the property to img,img content of 1 to image;$rootdata->setattribute ('img','Image'); $doc->save ('Data.xml'); } } } } ?>
PHP Modify, add XML node attribute implementation code