您的位置:首页 > 编程语言 > PHP开发

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;

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: