php xml修改节点中的属性和值
2016-05-20 18:09
711 查看
<?php
$xmlpatch = 'index.xml';
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$document = $doc -> documentElement;
$elm = $document -> getElementsByTagName('item');
$_id = '2';
$_title = 'url has been changed';
$_content = '内容has been changed';
$checkexist = 0;
foreach ($elm as $new) {
if($new -> getAttribute('id') == $_id) {
$op = $new -> getElementsByTagName('op');
//$op -> setAttribute('url', $_title);
//$op -> nodeValue = $_content;
foreach ($op as $opnew) {
$opnew -> setAttribute('url', $_title);
$opnew -> nodeValue = $_content;
}
$checkexist = 1;
}
}
if($checkexist == 0) {
echo $_id . ' is not found in ' . $xmlpatch;
} else {
$doc -> save($xmlpatch);
echo $_id . ' has been changed';
}
} else {
echo 'xml file loaded error!';
}
exit;
?>
$xmlpatch = 'index.xml';
$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$document = $doc -> documentElement;
$elm = $document -> getElementsByTagName('item');
$_id = '2';
$_title = 'url has been changed';
$_content = '内容has been changed';
$checkexist = 0;
foreach ($elm as $new) {
if($new -> getAttribute('id') == $_id) {
$op = $new -> getElementsByTagName('op');
//$op -> setAttribute('url', $_title);
//$op -> nodeValue = $_content;
foreach ($op as $opnew) {
$opnew -> setAttribute('url', $_title);
$opnew -> nodeValue = $_content;
}
$checkexist = 1;
}
}
if($checkexist == 0) {
echo $_id . ' is not found in ' . $xmlpatch;
} else {
$doc -> save($xmlpatch);
echo $_id . ' has been changed';
}
} else {
echo 'xml file loaded error!';
}
exit;
?>
相关文章推荐
- PHP新手之学习类与对象
- php变量详解
- PHPMailer发送邮件demo示例
- PHP在Windows环境下安装Imagick扩展
- PHP开发者应了解的24个库
- php 在原有xml文件中追加节点
- 8 个必备的PHP功能开发
- PHP 代码优化技巧大盘点
- ViewPager+Fragment最优实现方案
- php 创建xml文件
- php魔术方法
- 安装xdebug并配置 php插件
- php图片裁剪函数
- php 生成条形码(基于yii)
- 快还要更快,让PHP 7 运行更加神速
- mac下去掉php55换成php70
- FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
- php 查看代码执行时间
- PHP语法
- yii2-user 一个好用的用户扩展