php伪造xml,php处理xml

php对xml文件中节点的删除\编辑

使用getElementsByTagName()方法删除\编辑XML节点

三个文件:

1.root.xml

2.remove.php —— 删除节点

3.replace.php —— 替换节点

root.xml

child1 content

child2 content

child3 content

child1 content

child2 content

child3 content

remove.php

$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串

$doc = DOMDocument::loadXML($xml_str);  //载入这个字符串

//取得元素$child2

$root = $doc->documentElement;

$child2 = $root->getElementsByTagName("child2")->item(0);

//删除$child2对象

$root->removeChild($child2);

//保存文件

$doc->formatOutput = true;

$doc->saveXML();

$doc->save("root.xml");

?>

$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串

$doc = DOMDocument::loadXML($xml_str); //载入这个字符串

//取得元素$child2

$root = $doc->documentElement;

$child2 = $root->getElementsByTagName("child2")->item(0);

//删除$child2对象

$root->removeChild($child2);

//保存文件

$doc->formatOutput = true;

$doc->saveXML();

$doc->save("root.xml");

?>

replace.php

$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串

$doc = DOMDocument::loadXML($xml_str);  //载入这个字符串

//取得元素$child3

$root = $doc->documentElement;

$child3 = $root->getElementsByTagName("child3")->item(0);

//创建一个newchild元素来替代child3

$root->replaceChild(new DOMElement("newchild", "new content"), $child3);

//保存文件

$doc->formatOutput = true;

$doc->saveXML();

$doc->save("root.xml");

?>

$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读�

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值