Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中重命名XML DOM节点_Php_Xml_Dom - Fatal编程技术网

在PHP中重命名XML DOM节点

在PHP中重命名XML DOM节点,php,xml,dom,Php,Xml,Dom,如何重命名DOMDocument中的XML节点?在编写新节点之前,我想在XML文件中备份一个节点。我有这个代码,我想把URL节点重命名为URL\u备份 function backup_urls( $nodeid ) { $dom = new DOMDocument(); $dom->load('communities.xml'); $dom->formatOutput = true; $dom->preserveWhiteSpace = true; // get doc

如何重命名DOMDocument中的XML节点?在编写新节点之前,我想在XML文件中备份一个节点。我有这个代码,我想把URL节点重命名为URL\u备份

function backup_urls( $nodeid ) {

$dom = new DOMDocument();
$dom->load('communities.xml');

$dom->formatOutput = true; 
$dom->preserveWhiteSpace = true;

// get document element  

$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//COMMUNITY[@ID='$nodeid']"); 

if ($nodes->length) {

   $node = $nodes->item(0); 

   $xurls = $xpath->query("//COMMUNITY[@ID='$nodeid']/URLS");

   if ($xurls->length) {
   /* rename URLS to URLS_BACKUP */

   }

}

$dom->save('communities.xml');
}
XML文件具有这种结构

<?xml version="1.0" encoding="ISO-8859-1"?>
<COMMUNITIES>
 <COMMUNITY ID="c000002">
  <NAME>ID000002</NAME>
  <TOP>192</TOP>
  <LEFT>297</LEFT>
  <WIDTH>150</WIDTH>
  <HEIGHT>150</HEIGHT>
  <URLS>
     <URL ID="u000002">
         <NAME>Facebook.com</NAME>
         <URLC>http://www.facebook.com</URLC>
     </URL>
  </URLS>
 </COMMUNITY>
</COMMUNITIES>

ID000002
192
297
150
150
脸谱
http://www.facebook.com

谢谢。

您使用fopen阅读了整个xml文件列表,并使用了str_replace()方法


您使用fopen读取整个xml文件列表,并使用str_replace()方法


无法重命名DOM中的节点。字符串函数可能有效,但最好的解决方案是创建一个新节点并替换旧节点

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);

$nodeId = 'c000002'; 
$nodes = $xpath->evaluate("//COMMUNITY[@ID='$nodeid']/URLS");

// we change the document, iterate the nodes backwards
for ($i = $nodes->length - 1; $i >= 0; $i--) {
  $node = $nodes->item($i);
  // create the new node
  $newNode = $dom->createElement('URL_BACKUP');
  // copy all children to the new node
  foreach ($node->childNodes as $childNode) {
    $newNode->appendChild($childNode->cloneNode(TRUE));
  }
  // replace the node
  $node->parentNode->replaceChild($newNode, $node);
}

echo $dom->saveXml();

无法重命名DOM中的节点。字符串函数可能有效,但最好的解决方案是创建一个新节点并替换旧节点

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);

$nodeId = 'c000002'; 
$nodes = $xpath->evaluate("//COMMUNITY[@ID='$nodeid']/URLS");

// we change the document, iterate the nodes backwards
for ($i = $nodes->length - 1; $i >= 0; $i--) {
  $node = $nodes->item($i);
  // create the new node
  $newNode = $dom->createElement('URL_BACKUP');
  // copy all children to the new node
  foreach ($node->childNodes as $childNode) {
    $newNode->appendChild($childNode->cloneNode(TRUE));
  }
  // replace the node
  $node->parentNode->replaceChild($newNode, $node);
}

echo $dom->saveXml();

知道这是不可能的,这里有一个更简单的函数,用于在保存文件后重命名标记:

/**
 * renames a word in a file
 *
 * @param string $xml_path
 * @param string $orig word to rename
 * @param string $new new word
 * @return void
 */
public function renameNode($xml_path,$orig,$new)
{
    $str=file_get_contents($xml_path);
    $str=str_replace($orig, $new ,$str);
    file_put_contents($xml_path, $str);
}

知道这是不可能的,这里有一个更简单的函数,用于在保存文件后重命名标记:

/**
 * renames a word in a file
 *
 * @param string $xml_path
 * @param string $orig word to rename
 * @param string $new new word
 * @return void
 */
public function renameNode($xml_path,$orig,$new)
{
    $str=file_get_contents($xml_path);
    $str=str_replace($orig, $new ,$str);
    file_put_contents($xml_path, $str);
}

您必须使用replaceNode,但必须先创建新节点URL\u备份并克隆或将所有子节点复制到此节点窗体URLC@user900898因此,唯一的方法是创建节点及其嵌套内容的新副本。没有重命名。如果要删除旧节点,必须先复制,然后删除。谢谢。您必须使用replaceNode,但在创建新节点URL之前,请备份并克隆或将所有子节点复制到此节点窗体URLC@user900898因此,唯一的方法是创建节点及其嵌套内容的新副本。没有重命名。如果要删除旧节点,必须先复制,然后删除。谢谢。这是一个相当危险的方法。请永远不要使用它。这是一个相当危险的方法。请永远不要使用它。如果文本中的某个地方使用了“URL”怎么办?为什么一次只读取4012字节?如果所需的标记正好位于缓冲区的边界上,该怎么办<代码>##。如果文本中的某个地方使用了“URL”怎么办?为什么一次只读取4012字节?如果所需的标记正好位于缓冲区的边界上,该怎么办<代码>