Home>Article>Backend Development> Simple example sharing of adding data to xml file in php

Simple example sharing of adding data to xml file in php

墨辰丷
墨辰丷 Original
2018-06-01 11:49:14 1322browse

This article mainly introduces a simple example of adding data to an xml file in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

xml file: stu.xml:

    张三 80 90 70   李四 60 90 75   

The above file stu.xml provides partial *.

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:

    张三 80 90 70 240   李四 60 90 75 2225   

Use php to implement:

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 “节点已经存在了,不再添加!”; continue; } $zongfen = $yuwen+$shuxue+$yingyu; $newNode = $doc->createElement(“zongfen”,$zongfen); $stu->appendChild($newNode); } $result = $doc->saveXML(‘stu.xml'); ?>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php_pdo implements prepared statements

##phpeach returns the current key-value pair in the array and moves the array pointer one step forward

phpDetailed explanation and examples of the array processing function extract

The above is the detailed content of Simple example sharing of adding data to xml file in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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