更改 XML 节点值

发布于 2024-12-01 10:15:35 字数 1504 浏览 0 评论 0原文

此脚本应更改指定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-12-08 10:15:35

首先,您应该调整获取所需 COMMUNITY 元素的代码。 xpath 方法返回一个 SimpleXMLElement 对象数组,表示与搜索字符串匹配的元素。

示例:

$returnArray = $nodes->xpath("//COMMUNITY[@ID='$nodeid']"); 
$node = $returnArray[0];

其次,要修改 TOP 元素的值,直接设置为 TOP 本身:

// This replaces the contents of the TOP element with $top
$node->TOP = $top;

// This appends '<nodeValue>$top</nodeValue>' to the contents of TOP
$node->TOP->nodeValue = $top;

First, you should adjust the code that gets the desired COMMUNITY element. The xpath method returns an array of SimpleXMLElement objects, representing the elements that matched your search string.

Example:

$returnArray = $nodes->xpath("//COMMUNITY[@ID='$nodeid']"); 
$node = $returnArray[0];

Second, to modify the value of the TOP element, set directly to TOP itself:

// This replaces the contents of the TOP element with $top
$node->TOP = $top;

// This appends '<nodeValue>$top</nodeValue>' to the contents of TOP
$node->TOP->nodeValue = $top;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文