0

This is the 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>

I want to change the "TABLE-RECORDS" to "PRODUCTS" and delete the "EXPORT-RECORDS" tag.

I've tried using the SimpleXML, but all I can retrieve with that is the name of the tag using getName() and the DOMDocument is simply not working for me.

ANY help/guidance would be much appreciated!

  • Why not just create a new XML with desired root node? Result is the same but it's much less complicated. – ToBe Jan 21 '14 at 09:41
  • possible duplicate of [Remove a child with a specific attribute, in SimpleXML for PHP](http://stackoverflow.com/questions/262351/remove-a-child-with-a-specific-attribute-in-simplexml-for-php) – Þaw Jan 21 '14 at 09:53
  • @ToBe -- I will need to automate this process as it will be the xml file for uploading the entire catalog. – JessicaRod Jan 21 '14 at 17:59
  • @Þaw -- I have looked at the link provided, however it's removing an attribute and I need to remove an element. correct me if I'm wrong please. – JessicaRod Jan 21 '14 at 18:00
  • I still dont see your problem. Do you want to automate removing/changing other root nodes or do you want to change remove any other nodes inside an XML? If it's allways root, allways just create a new one. If it's something else, SimpleXMLElement provides easy functions for that. – ToBe Jan 22 '14 at 08:45
  • @ToBe I will be downloading this file from a server daily to update products. The file always will have this structure, which means I will manually have to open the file rename/remove the tags in question on a daily basis. I thought writing a php script to do that would be beneficial. – JessicaRod Jan 22 '14 at 09:06
  • @JessicaRod As you are still basically just deleting the whole xml file and creating a new one, I cant see the reason why you download it before that. If you are deleting it, why not just create a new empty one without downloading it? Are you really just renaming and emtying the root node? - Oh, dou you maybe want to move all PRODUCT nodes up one leven into the renamed TABLE_RECORDS node? Your info was a bit unclear on that... – ToBe Jan 22 '14 at 11:13
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Touki Feb 28 '14 at 11:31

1 Answers1

0

If you want to move a node to another place, you might want to try these functions. 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);

Can you work from there and work out the missing parts yourself?

ToBe
  • 2,667
  • 1
  • 18
  • 30
  • I'm not sure how to go about doing the loop part...however, here is the code that gets me the names of the first two nodes, "TABLE-RECORDS" and "EXPORT-RECORDS". `$xml_file = 'products2.xml';` `$xmlobj = simplexml_load_file($xml_file);` `$first = $xmlobj->getName();` `foreach ($xmlobj->children() as $child) {` `echo 'sec: ' . $child->getName() . "\n";` `}` `$xmlobj->asXML($xml_file); ` – JessicaRod Jan 22 '14 at 22:07
  • This is where I get stuck. $first has the TABLE-RECORDS which I want to change its name to PRODUCTS and $child->getName() has EXPORT-RECORDS which I need to delete. – JessicaRod Jan 22 '14 at 22:13
  • You need to do these steps in order: 1) find source node "PRODUCT". 2) iterate through all it's children and move them to root node "TABLE-RECORDS" 3) delete source node 4) Rename root node. I fear you will have to learn to use DomDocument and DomNode classes for this to work. – ToBe Jan 23 '14 at 09:05
  • ok, I will try it out and let you know...thanks for your guidance! :) – JessicaRod Jan 23 '14 at 22:34
  • here is what I came up with:- 1 To get the Element -- `$nodes = $dom->getElementsByTagName('TABLE-RECORDS');` 2- iterate through nodes `foreach ($nodes as $node) { $newNode = $dom->createElement($new);` --- Here is where I am stuck how do I move them ?? – JessicaRod Jan 24 '14 at 20:02
  • I thought you wanted to move the nodes to the renamed root node, not create any node? I think you got the iteration right, the moving part is shown above. remove the node to move from it's current parent and then insert it at it's new parent. – ToBe Jan 27 '14 at 08:41
  • I think you want to iterate over all children of "PRODUCTS", not over the one "TABLE-RECOEDS" node you have? How about: `$products = array_pop($dom->getElementsByTagName('PRODUCT'));` and then iterate over `$products->children()` Sorry but not enough time atm to write actual code... – ToBe Jan 27 '14 at 08:42