找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

php怎么改变xml 节点值

作者:模板之家 2021-9-2 17:09 71人关注

php改变xml节点值的方法:1、从数据库读取数据;2、写一个xml文件;3、创建DOMDocument的对象并载入xml文件;4、修改指定节点下子节点的值即可。

php改变xml节点值的方法:1、从数据库读取数据;2、写一个xml文件;3、创建DOMDocument的对象并载入xml文件;4、修改指定节点下子节点的值即可。

本文操作环境:Windows7系统、PHP7.1版本、Dell G3电脑

php怎么改变xml 节点值?

php修改xml节点的值

今天刚刚实现的功能,找了很多资料,本来想用xpath,但是由于对xpath不甚了解。绕了个大圈后还是决定用DOMDocument来做。

在此做一个标记,以后找资料的时候也不用太辛苦。

先从数据库读取数据,然后写一个xml文件。xml文件格式如下。

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>

php文件里的处理。

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

$_GET['id']和$_GET['content']是ajax传过来的参数。

推荐学习:《PHP视频教程》

以上就是php怎么改变xml 节点值的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)