更改 XML 节点值
此脚本应更改指定 COMMUNITY 节点的 TOP 值。但它并没有什么问题?
<?php
function make_update( $nodeid, $name, $top, $left, $width, $height ) {
$nodes = new SimpleXMLElement('communities.xml', null, true);
$node = $nodes->xpath("//COMMUNITY[@ID='$nodeid']");
$node->TOP->nodeValue = $top;
$nodes->asXML();
return $top;
}
echo make_update(trim($_REQUEST['nodeid']),trim($_REQUEST['name']),trim($_REQUEST['top']),trim($_REQUEST['left']),trim($_REQUEST
['width']),trim($_REQUEST['height']));
?>
XML 看起来像这样。
<?xml version="1.0" encoding="ISO-8859-1"?>
<COMMUNITIES>
<COMMUNITY ID="c001">
<NAME>Town Services</NAME>
<TOP>50</TOP>
<LEFT>50</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>300</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Google.com</NAME>
<URLC>http://www.google.com</URLC>
</URL>
<URL ID="U002">
<NAME>Bing.com</NAME>
<URLC>http://www.bing.com</URLC>
</URL>
<URL ID="U003">
<NAME>Yahoo.com</NAME>
<URLC>http://www.yahoo.com</URLC>
</URL>
<URL ID="U004">
<NAME>Aol.com</NAME>
<URLC>http://www.aol.com</URLC>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
This script should change the TOP value of the specified COMMUNITY node. But it doesn't what could be wrong?
<?php
function make_update( $nodeid, $name, $top, $left, $width, $height ) {
$nodes = new SimpleXMLElement('communities.xml', null, true);
$node = $nodes->xpath("//COMMUNITY[@ID='$nodeid']");
$node->TOP->nodeValue = $top;
$nodes->asXML();
return $top;
}
echo make_update(trim($_REQUEST['nodeid']),trim($_REQUEST['name']),trim($_REQUEST['top']),trim($_REQUEST['left']),trim($_REQUEST
['width']),trim($_REQUEST['height']));
?>
The XML looks like this.
<?xml version="1.0" encoding="ISO-8859-1"?>
<COMMUNITIES>
<COMMUNITY ID="c001">
<NAME>Town Services</NAME>
<TOP>50</TOP>
<LEFT>50</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>300</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Google.com</NAME>
<URLC>http://www.google.com</URLC>
</URL>
<URL ID="U002">
<NAME>Bing.com</NAME>
<URLC>http://www.bing.com</URLC>
</URL>
<URL ID="U003">
<NAME>Yahoo.com</NAME>
<URLC>http://www.yahoo.com</URLC>
</URL>
<URL ID="U004">
<NAME>Aol.com</NAME>
<URLC>http://www.aol.com</URLC>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该调整获取所需
COMMUNITY
元素的代码。xpath
方法返回一个SimpleXMLElement
对象数组,表示与搜索字符串匹配的元素。示例:
其次,要修改
TOP
元素的值,直接设置为TOP
本身:First, you should adjust the code that gets the desired
COMMUNITY
element. Thexpath
method returns an array ofSimpleXMLElement
objects, representing the elements that matched your search string.Example:
Second, to modify the value of the
TOP
element, set directly toTOP
itself: