Home Backend Development PHP Tutorial PHP implementation code to modify and add xml node attributes_PHP tutorial

PHP implementation code to modify and add xml node attributes_PHP tutorial

Jul 13, 2016 pm 05:17 PM
php xml code Revise Increase accomplish Attributes have Node

PHP modifies xml node attributes and adds code for xml node attributes. Friends in need can refer to it

PHP modified to add the code of xml node attributes for your reference.
PHP modifies xml node attributes and adds code for adding xml node attributes. For those who need it, please refer to it.

1. xml file

Copy the code as follows:


<?xml version="1.0" encoding="UTF-8" ?>
<clientSet>
<server url="192.168.0.180" port="1935" />
<rootPath value="" />
<homePath value="http://www.jbxue.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>
Copy after login

2.php code

<?
$dom=new DOMDocument(&#39;1.0&#39;);
$dom->load(&#39;x.xml&#39;);
$em=$dom->getElementsByTagName(&#39;emotions&#39;);
$em=$em->item(0);
$items=$em->getElementsByTagName(&#39;item&#39;);
foreach($items as $a){
foreach($a->attributes as $b){
if($b->nodeValue==&#39;Birthday&#39;){
$a->setAttribute(&#39;name&#39;,&#39;nBirthday&#39;);
}
}
}
$t=$dom->createElement(&#39;item&#39;);
$t->setAttribute(&#39;name&#39;,&#39;x&#39;);
$t->setAttribute(&#39;src&#39;,&#39;www.baidu.com&#39;);
$t->setAttribute(&#39;duration&#39;,&#39;duration&#39;);
$em->appendChild($t);
$dom->save(&#39;x.xml&#39;);
?>
Copy after login

PHP parses XML document attributes and edits

<?php 

//读取xml 
 $dom=new DOMDocument(&#39;1.0&#39;); 
$dom->load(&#39;data.xml&#39;); 
$em=$dom->getElementsByTagName(&#39;videos&#39;);//最外层节点 
$em=$em->item(0); 
$items=$em->getElementsByTagName(&#39;video&#39;);//节点 
//如果不用读取直接添加的话把下面这一段去掉即可 
foreach($items as $a){ 
foreach($a->attributes as $b){//$b->nodeValue;节点属性的值$b->nodeName;节点属性的名称 
 echo $b->nodeName; 
 echo ":";  // www.jbxue.com
 echo $b->nodeValue; 
 echo "<br/>"; 
} 
} 
//下面是往xml写入一行新的 
$t=$dom->createElement(&#39;video&#39;);//<video 
$t->setAttribute(&#39;title&#39;,&#39;1&#39;);//<video name="data" 
$t->setAttribute(&#39;src&#39;,&#39;2&#39;);//<video name="data" src="2" 
$t->setAttribute(&#39;img&#39;,&#39;1&#39;);//<video name="data" img="1" 
$em->appendChild($t);//<video name="data" img="1"/> 
$dom->save(&#39;data.xml&#39;); 
?>  
Copy after login

The xml document at that 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> 
Copy after login


//If you change later, you can modify the xml
<?php 

$doc = new DOMDocument(); 
$doc->load(&#39;data.xml&#39;); 

//查找 videos 节点 
$root = $doc->getElementsByTagName(&#39;videos&#39;); 

//第一个 videos 节点 
$root = $root->item(0); 

//查找 videos 节点下的 video 节点 
$userid = $root->getElementsByTagName(&#39;video&#39;); 

//遍历所有 video 节点 
foreach ($userid as $rootdata) 
{ 
//遍历每一个 video 节点所有属性 
foreach ($rootdata->attributes as $attrib) 
{ 
$attribName = $attrib->nodeName;   //nodeName为属性名称 
$attribValue = $attrib->nodeValue; //nodeValue为属性内容 

//查找属性名称为ip的节点内容 
if ($attribName ==&#39;img&#39;) 
{ 
//查找属性内容为ip的节点内容 
if ($attribValue ==&#39;1&#39;) 
{ 
//将属性为img,img内容为1的修改为image; 
$rootdata->setAttribute(&#39;img&#39;,&#39;image&#39;); 
$doc->save(&#39;data.xml&#39;); 
} 
} 
} 
}  
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/625812.htmlTechArticlephp modifies xml node attributes and adds code for xml node attributes. Friends in need can refer to the php modification Add the code for xml node attributes for your reference. PHP modifies xml node attributes...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1251
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles