Home  >  Article  >  Backend Development  >  How to change xml node value in php

How to change xml node value in php

藏色散人
藏色散人Original
2021-09-02 09:26:512139browse

php method to change the value of an xml node: 1. Read data from the database; 2. Write an xml file; 3. Create a DOMDocument object and load the xml file; 4. Modify the value of the child node under the specified node That’s it.

How to change xml node value in php

The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to change the xml node value in php?

php modifies the value of xml node

The function I just implemented today, I found a lot of information, I originally wanted to use xpath, but I don’t know much about xpath. After going around in a big circle, I decided to use DOMDocument.

Make a mark here so you don’t have to work too hard to find information in the future.

First read the data from the database and then write an xml file. The xml file format is as follows.

mainchart.xml

<?xml version="1.0" encoding="utf-8"?>
<records>
<record>
<pono>5008171</pono>
<status>3</status>
<opentime>2010.06.13 14:19</opentime>
<closetime>2010.06.16 14:19</closetime>
<potype>balance</potype>
<variety/>
<margin/>
<openprice/>
<closeprice/>
<zhisun/>
<zhiying/>
<lowest/>
<highest/>
<netvalue/>
<openamount/>
<openinterest/>
<amount/>
<point/>
<positiontime>3</positiontime>
<memo>TRMM-DP(123005)-D</memo>
</record>
<record>
<pono>5011083</pono>
<status>3</status>
<opentime>2010.06.15 16:15</opentime>
<closetime>2010.06.15 16:23</closetime>
<potype>buy</potype>
<variety>eurusd</variety>
<margin/>
<openprice>1.31822</openprice>
<closeprice>1.31655</closeprice>
<zhisun>0</zhisun>
<zhiying>0</zhiying>
<lowest/>
<highest/>
<netvalue/>
<openamount/>
<openinterest/>
<amount/>
<point/>
<positiontime>00:08:00</positiontime>
<memo>aaafff</memo>
</record>
<record>
<pono>5011913</pono>
<status>3</status>
<opentime>2010.06.15 16:51</opentime>
<closetime>2010.06.15 17:19</closetime>
<potype>sell</potype>
<variety>eurusd</variety>
<margin/>
<openprice>1.31819</openprice>
<closeprice>1.31809</closeprice>
<zhisun>0</zhisun>
<zhiying>0</zhiying>
<lowest/>
<highest/>
<netvalue/>
<openamount/>
<openinterest/>
<amount/>
<point/>
<positiontime>00:28:00</positiontime>
<memo>eee</memo>
</record>
</records>

Processing in php file.

$file ="mainchart.xml";
    //创建DOMDocument的对象
    $dom=new DOMDocument(&#39;1.0&#39;);
    //载入mainchart.xml文件
    $dom->load($file);
    //获得record节点的集合
    $records = $dom->getElementsByTagName(&#39;record&#39;);
    //遍历record节点的集合
    foreach($records as $record){
        //如果record节点的pono子节点的值满足条件,就修改该record节点下memo子节点的值
        if($record->getElementsByTagName(&#39;pono&#39;)->item(0)->nodeValue == $_GET[&#39;id&#39;]){
            $record->getElementsByTagName(&#39;memo&#39;)->item(0)->nodeValue = $_GET[&#39;content&#39;];
        }
    }
$dom->save(&#39;mainchart.xml&#39;);

$_GET['id'] and $_GET['content'] are parameters passed by ajax.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to change xml node value 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