Home > php教程 > php手册 > body text

Example of adding data to xml file in php

WBOY
Release: 2016-09-09 08:13:35
Original
1268 people have browsed it

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:




Zhang San
< yuwen>80
90
70


李思
60
90
75


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:




Zhang San
< yuwen>80
90
70
240

& lt;xuesheng>
< ;name>李思
60
90
75
2225


Use php to implement:

Copy the codeThe code is as follows:
$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') ;
?>
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template