Table of Contents
php adds data to xml file
Related articles
Home php教程 php手册 Example of adding data to xml file in php

Example of adding data to xml file in php

Sep 09, 2016 am 08:13 AM

php 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.
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>
&lt ;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') ;
?>
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 尊渡假赌尊渡假赌尊渡假赌

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 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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)