PHP to modify, add XML node attributes to achieve code _php skills

Source: Internet
Author: User

PHP to modify the code to increase the XML node attributes, for everyone to learn the reference.
PHP modifies XML node attributes, adds XML node attributes to the code, has the need for friends, reference.

1. xml file

Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<clientSet>
<server url= "192.168.0.180" port= "1935"/>
<rootpath value= ""/>
<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= "15" />
<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

Copy Code code as follows:

?
$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

Copy Code code as follows:

<?php
Reading XML
$dom =new DOMDocument (' 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 the direct addition of the following paragraph can be removed
foreach ($items as $a) {
foreach ($a->attributes as $b) {//$b the name of the value $b->nodename; node property of the->nodevalue; node property
Echo $b->nodename;
echo ":";
Echo $b->nodevalue;
echo "<br/>";
}
}
The following is a new line written to XML
$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 ');
?>

The XML document at that time:
Copy Code code as follows:

<?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"/>
<video title= "D" src= "2" img= "1"/>
</videos>

The following file is modified to modify the XML

Copy Code code as follows:

<?php
$doc = new DOMDocument ();
$doc->load (' Data.xml ');

Find Videos Node
$root = $doc->getelementsbytagname (' videos ');

First Videos node
$root = $root->item (0);

Find a video node under the videos node
$userid = $root->getelementsbytagname (' video ');

Traverse All video nodes
foreach ($userid as $rootdata)
{
Traverse every video node for all properties
foreach ($rootdata->attributes as $attrib)
{
$attribName = $attrib->nodename; NodeName as Property name
$attribValue = $attrib->nodevalue; NodeValue as property content

Find the contents of a node with a property name of IP
if ($attribName = = ' img ')
{
Find the content of the node that has the property content as IP
if ($attribValue = = ' 1 ')
{
Change the property to img,img content of 1 to image;
$rootdata->setattribute (' img ', ' image ');
$doc->save (' Data.xml ');
}
}
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.