php中怎么修改XML中某个结点的值

wsdzmhongm 2006-07-11 03:02:11
比如我要修改 <title>印度试射可携带核弹头烈火-3型导弹失败</title>为<title>xxxxxx</title>
PHP里有这样的xml操作函数吗?
<?xml version="1.0" encoding="utf-8"?>
<news>
<item id="1">
<title>印度试射可携带核弹头烈火-3型导弹失败</title>
<link>http://news.sina.com.cn/w/2006-07-10/092010379228.shtml</link>
<author>WWW.SINA.COM.CN</author>
<guid>http://news.sina.com.cn/w/2006-07-10/092010379228.shtml</guid>
<category></category>
<pubDate>Mon, 10 Jul 2006 01:20:10 GMT</pubDate>
<comments></comments>
<description>  中新网7月10日电 据新加坡《联合早报》报道,印度9日第一次试射能携带核弹头的“烈火-3”型(Agni-III)中程弹道导弹,不过试射以失败收场。

  这枚射程长达4000公里的导弹在当地时间9日上午11时03分(北京时间9日下午2时35分)从印度东海岸奥里萨州的惠勒岛发射,发射地点距....</description>
</item>

<item id="2">
<title>银川发生液氯泄漏事故160多人中毒</title>
<link>http://news.sina.com.cn/c/2006-07-10/084310378792.shtml</link>
<author>WWW.SINA.COM.CN</author>
<guid>http://news.sina.com.cn/c/2006-07-10/084310378792.shtml</guid>
<category></category>
<pubDate>Mon, 10 Jul 2006 00:43:31 GMT</pubDate>
<comments></comments>
<description>  新华网银川7月10日电 (记者武勇) 7月9日晚,宁夏回族自治区银川市西夏区鑫尔特化学品有限公司发生液氯泄漏事故。截至10日早晨8时,已有164人被送往当地两家医院救治,其余中毒人员还在排查之中。

  银川市第一人民医院院长姚成立说,7月9日晚21时许,有人反映银川市西夏....</description>
</item>
</news>

...全文
877 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wsdzmhongm 2006-07-11
  • 打赏
  • 举报
回复
把 echo $dom- >saveXML(); 改为 dom- >save('xml文件名'); 就可以保存文件了
wsdzmhongm 2006-07-11
  • 打赏
  • 举报
回复
Here is a simple example for replacing a node:

Let's define our XML like so:

<?php
$xml = <<<XML
<?xml version="1.0"?>
<root>
<parent>
<child>bar</child>
<child>foo</child>
</parent>
</root>
XML;?>

If we wanted to replace the entire <parent> node, we could do something like this:

<?php
// Create a new document fragment to hold the new <parent> node
$parent = new DomDocument;
$parent_node = $parent ->createElement('parent');

// Add some children
$parent_node->appendChild($parent->createElement('child', 'somevalue'));
$parent_node->appendChild($parent->createElement('child', 'anothervalue'));

// Add the keywordset into the new document
// The $parent variable now holds the new node as a document fragment
$parent->appendChild($parent_node);
?>

Next, we need to locate the old node:

<?php
// Load the XML
$dom = new DomDocument;
$dom->loadXML($xml);

// Locate the old parent node
$xpath = new DOMXpath($dom);
$nodelist = $xpath->query('/root/parent');
$oldnode = $nodelist->item(0);
?>

We then import and replace the new node:

<?php
// Load the $parent document fragment into the current document
$newnode = $dom->importNode($parent->documentElement, true);

// Replace
$oldnode->parentNode->replaceChild($newnode, $oldnode);

// Display
echo $dom->saveXML();
?>

Our new node is successfully imported:

<?xml version="1.0"?>
<root>
<parent><child>somevalue</child><child>anothervalue</child></parent>
</root>
johnpanq 2006-07-11
  • 打赏
  • 举报
回复
用php5.0的 DOMDocument来处理。
mynamesucks 2006-07-11
  • 打赏
  • 举报
回复
接分哈哈
anshenghao 2006-07-11
  • 打赏
  • 举报
回复
你要用php修改xml然后保存是么?
如果是那也可以按我上面说的那样
先read()然后改就可以了!
wsdzmhongm 2006-07-11
  • 打赏
  • 举报
回复
快来,我要结帖了
wsdzmhongm 2006-07-11
  • 打赏
  • 举报
回复
我找到了
wsdzmhongm 2006-07-11
  • 打赏
  • 举报
回复
我是要修改xml文件
anshenghao 2006-07-11
  • 打赏
  • 举报
回复
似乎你在用php解吸xml的时候 在读到<title>和</title>中间的时候可以修改
应该并不难
wsdzmhongm 2006-07-11
  • 打赏
  • 举报
回复
我对PHP不是很了解,请大家帮忙

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧