-2

I have a problem that someone might be able to help me with. I am not an experienced programmer but I am ok to have advanced answers so I can learn more.

I download an xml file, open it and read it. When a specific condition is met I change value in that node and this works fine. But after that I'd like to write the whole node that I just changed into a new xml file and this is where I've got problems. I manage to store the last node which indicates that I am writing over existing node in the new document all the time.

This is my code:

$prodsToSave = new DOMDocument(); //my new document
$prods = new DOMDocument(); 
$prods = simplexml_load_file('http://urltofile.com');

foreach ($prods as $product) {
    if($product->merchantCategoryName == 'CD') { 
        $productToSave = $product;
        $productToSave->merchantCategoryName = 'Musik > CD';
        $prodsToSave=$productToSave;
    }
    $prodsToSave->saveXML('newdocument.xml');
}

I can see why I only have one node written into the new document but I don't know how to change it so I get all my nodes in the new document?

M8R-1jmw5r
  • 4,896
  • 2
  • 18
  • 26
Lars
  • 1
  • 1
    why are u replacing you domdocument ($prods) with a simplexml object ? this has nothing to do with eachother.. – Yordi Apr 30 '13 at 08:15
  • Duplicate, check this question http://stackoverflow.com/questions/6001923/php-domdocument-question-how-to-replace-text-of-a-node. FYI, [DOMDocument::saveXML](http://www.php.net/manual/en/domdocument.savexml.php) doesn't accept filename as an argument, it just returns the XML for a given object. In order to write it to file use a function like [file_put_contents](http://www.php.net/manual/en/function.file-put-contents.php). – Rolando Isidoro Apr 30 '13 at 08:36
  • So, Yordi, How should it be to function, any ideas? Rolando: I do not agree, it is not a duplicate but if you think it is, please, take that information and apply to my question and answer it :) – Lars Apr 30 '13 at 08:43
  • I will look into the file_put_content function though, thank you! – Lars Apr 30 '13 at 08:52

1 Answers1

0

Setting a variable and then overwriting it directly again is superfluous:

 $a = 'something really cool';
 $a .= ' and awsome.';
 $a = $b;

You can greatly condense this code just to the last line:

 $a = $b;

So what is left?

$prods = simplexml_load_file('http://urltofile.com');

foreach ($prods as $product) {
    if ($product->merchantCategoryName != 'CD') { 
        continue;
    }
    $product->merchantCategoryName = 'Musik > CD';
    $product->saveXML('newdocument.xml');
}

If you reduce the code, it is also easier for you to read and understand it. So you should take your time from time to time to review and reduce your code your own.

I hope this is helpful for you.

M8R-1jmw5r
  • 4,896
  • 2
  • 18
  • 26
  • Thanks for all answers but I am afraid I still can't make it work. I wonder, is it easier to delete the rest for the existing xml file insted of writing a new one? Something like: if($product->merchantCategoryName == 'CD') { $productToSave->merchantCategoryName = 'Musik > CD'; } //And after checking all if statements simply remove those nodes that have not been changed. Anyone out there that knows a how to make that happend? Cheers! – Lars Apr 30 '13 at 14:08
  • @Lars: I would say this depends on context. If you couldn't make it to work, it indeed might be easier for you to go with your suggestion to delete everything else. In the end, you can just save to the same or a different file-name. So you can do it like you want to. – hakre May 02 '13 at 14:00