如何使用PHP重命名xml的根节点并删除它的子节点 - 初学者

时间:2014-01-21 09:32:33

标签: php xml root

这是XML FILE:products.xml

<?xml version="1.0"?>
<TABLE-RECORDS>
<EXPORT-RECORDS>
<PRODUCT>
  <PRODUCTS_NAME>Name of product</PRODUCTS_NAME>
  <PRODUCTS_DESCRIPTION>Description of Product</PRODUCTS_DESCRIPTION>
  <PRODUCTS_IMAGE>any.jpg</PRODUCTS_IMAGE>
  <PRODUCTS_PRICE>1.00</PRODUCTS_PRICE>
  <PRODUCTS_MODEL>ANY</PRODUCTS_MODEL>
  <ITEM_BRANDNAME>ANY</ITEM_BRANDNAME>
  <ITEM_UPC>11111111</ITEM_UPC>
  <ITEM_HEIGHT>1.00</ITEM_HEIGHT>
  <ITEM_LENGTH>1.00</ITEM_LENGTH>
  <ITEM_DIAMETER>1.00</ITEM_DIAMETER>
  <PRODUCTS_WEIGHT>1.00</PRODUCTS_WEIGHT>
  <MANUFACTURERS_NAME>any manufacturer</MANUFACTURERS_NAME>
  <ITEM_VENDOR_NUMBER>Vendor</ITEM_VENDOR_NUMBER>
  <PRODUCTS_QUANTITY>1</PRODUCTS_QUANTITY>
  <DATE_RECIEVED>0000-00-00 00:00:00</DATE_RECIEVED>
  <PROP_PACKAGING>box</PROP_PACKAGING>
  <PRODUCT_CLASS>any class</PRODUCT_CLASS>
  <PRODUCTS_TYPE>any type</PRODUCTS_TYPE>
</PRODUCT>
</EXPORT-RECORDS>
</TABLE-RECORDS>

我想将“TABLE-RECORDS”更改为“PRODUCTS”并删除“EXPORT-RECORDS”标记。

我尝试过使用SimpleXML,但我可以使用getName()查找标签的名称,而DOMDocument根本不适用于我。

非常感谢任何帮助/指导!

1 个答案:

答案 0 :(得分:0)

如果要将节点移动到其他位置,可能需要尝试这些功能。 http://www.php.net/manual/de/class.domdocument.php

//Load your xml into a DomDocument
$dom= new DOMDocument(); 
$dom->loadXML($xml); 

// Get the node in question (probably within a loop and remove from source
// This sample is missing the loop part where you select/load the $node!
$node = $node->parentNode->removeChild($node);

// Now select your target node and then mov
$node = $targetNode->insertBefore($node);

你可以在那里工作并自己解决缺失的部分吗?