php modifies and adds xml node attribute implementation code
- 2020-10-07 18:35:39
- OfStack
php modifies the code that adds xml node attribute, for your reference.
php modifies the xml node attribute and adds the code of xml node attribute. If you need it, please refer to the following.
1. xml file
<?xml version="1.0" encoding="UTF-8" ?>
<clientSet>
<server url="192.168.0.180" port="1935" />
<rootPath value="" />
<homePath value="http://www.aaa.com" />
<helpPath value="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="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="13"/>
<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 the XML document properties and edits them
<?php
// read 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
// Add the following without reading directly 1 Segment removed
foreach($items as $a){
foreach($a->attributes as $b){//$b->nodeValue; The value of a node property $b->nodeName; The name of the node property
echo $b->nodeName;
echo ":";
echo $b->nodeValue;
echo "<br/>";
}
}
// The following is the xml write 1 New 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 documents 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"/>
<video title="d" src="2" img="1"/>
</videos>
// The following file is modified later. xml can be modified
<?php
$doc = new DOMDocument();
$doc->load('data.xml');
// To find the videos node
$root = $doc->getElementsByTagName('videos');
// The first 1 a videos node
$root = $root->item(0);
// To find the videos Under the node video node
$userid = $root->getElementsByTagName('video');
// Iterate over all video node
foreach ($userid as $rootdata)
{
// Traverse every 1 a video Node all attributes
foreach ($rootdata->attributes as $attrib)
{
$attribName = $attrib->nodeName; //nodeName Is the attribute name
$attribValue = $attrib->nodeValue; //nodeValue Is attribute content
// The lookup property name is ip Node content of
if ($attribName =='img')
{
// The find property content is ip Node content of
if ($attribValue =='1')
{
// The property of img . img Content is 1 Modifications to image;
$rootdata->setAttribute('img','image');
$doc->save('data.xml');
}
}
}
}
?>