Table of Contents
Example of adding data to xml file in php
Sep 09, 2016 am 08:13 AMphp adds data to xml file
- Time: 2015-12-17 06:30:37 Source: Internet
Introduction: php adds data to xml file
xml file: stu.xml:
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<AllData>
<xueshengmen>
<xuesheng>
<name>Zhang San</name>
< yuwen>80</yuwen>
<shuxue>90</shuxue>
<yingyu>70</yingyu>
</xuesheng>
<xuesheng>
<name>李思</ name>
<yuwen>60</yuwen>
<shuxue>90</shuxue>
<yingyu>75</yingyu>
</xuesheng>
</xueshengmen>
</All Data>
The above file stu.xml provides some student data. <AllData>
<xueshengmen>
<xuesheng>
<name>Zhang San</name>
< yuwen>80</yuwen>
<shuxue>90</shuxue>
<yingyu>70</yingyu>
</xuesheng>
<xuesheng>
<name>李思</ name>
<yuwen>60</yuwen>
<shuxue>90</shuxue>
<yingyu>75</yingyu>
</xuesheng>
</xueshengmen>
</All Data>
Now you need to add a zongfen attribute to each xuesheng node and its value is equal to the value of yuwen+shuxue+yingyu
as shown below:
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<AllData>
<xueshengmen>
<xuesheng>
<name>Zhang San</name>
< yuwen>80</yuwen>
<shuxue>90</shuxue>
<yingyu>70</yingyu>
<zongfen>240</zongfen>
</xuesheng>
& lt;xuesheng>
< ;name>李思</name>
<yuwen>60</yuwen>
<shuxue>90</shuxue>
<yingyu>75</yingyu>
<zongfen>2225</zongfen >
</xuesheng>
</xueshengmen>
</AllData>
<AllData>
<xueshengmen>
<xuesheng>
<name>Zhang San</name>
< yuwen>80</yuwen>
<shuxue>90</shuxue>
<yingyu>70</yingyu>
<zongfen>240</zongfen>
</xuesheng>
& lt;xuesheng>
< ;name>李思</name>
<yuwen>60</yuwen>
<shuxue>90</shuxue>
<yingyu>75</yingyu>
<zongfen>2225</zongfen >
</xuesheng>
</xueshengmen>
</AllData>
Use php to implement:
Copy the codeThe code is as follows:
<?php
$doc = new DOMDocument();
$doc->load( 'stu.xml' );
$students = $doc->getElementsByTagName( “xuesheng” );
foreach($students as $stu){
$yuwen = $stu->getElementsByTagName( “yuwen” )->item(0)->nodeValue;
$shuxue = $stu->getElementsByTagName( “shuxue” )-> item(0)->nodeValue;
$yingyu = $stu->getElementsByTagName( “yingyu” )->item(0)->nodeValue;
$zongfen = $stu->getElementsByTagName( “zongfen” )->item(0)->nodeValue;
if($zongfen){
echo "The node already exists, no more adding!";
continue;
}
$zongfen = $yuwen+$shuxue+$yingyu;
$newNode = $doc->createElement("zongfen",$zongfen);
$stu->appendChild($newNode);
}
$result = $doc->saveXML('stu.xml') ;
?>
$doc = new DOMDocument();
$doc->load( 'stu.xml' );
$students = $doc->getElementsByTagName( “xuesheng” );
foreach($students as $stu){
$yuwen = $stu->getElementsByTagName( “yuwen” )->item(0)->nodeValue;
$shuxue = $stu->getElementsByTagName( “shuxue” )-> item(0)->nodeValue;
$yingyu = $stu->getElementsByTagName( “yingyu” )->item(0)->nodeValue;
$zongfen = $stu->getElementsByTagName( “zongfen” )->item(0)->nodeValue;
if($zongfen){
echo "The node already exists, no more adding!";
continue;
}
$zongfen = $yuwen+$shuxue+$yingyu;
$newNode = $doc->createElement("zongfen",$zongfen);
$stu->appendChild($newNode);
}
$result = $doc->saveXML('stu.xml') ;
?>
Related articles
- PHP operates XML class DOMDocument
- php generates simple xml file
- Example code learning for generating xml files with PHP
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 Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
