1

I want to write a PHP script that will modify my XML file.

I have my productId within the node as an attribute and I want to parse the entire file and convert it to a separate node. So I want to read the attribute of the node and put that attribute in its own node. But the rest of the nodes will stay as is.

Before:

<product id="123">
<name>bob</name>
<lastname>tim</lastname>
</product>

To:

<product>
<id>123</id>
<name>bob</name>
<lastname>tim</lastname>
</product>

Can I do this in PHP? Bearing in mind the file will have over one thousand separate products in it.

  • 1
    Yes, it is. Here's an example: http://stackoverflow.com/questions/10239298/how-to-convert-xml-attributes-to-text-nodes – nomistic Jul 15 '15 at 23:49
  • Next to the duplicate, it makes not much sense to ask on a Q&A site if "this can be done?". Because the answer most often is either a simple "Yes" or an "it depends". I guess that's not useful neither for the person who asks nor for future visitors. Just saying :D – hakre Jul 16 '15 at 06:06
  • @hakre this is a more unique situation compared to the duplicate you flagged if you like I can update the title but as you can see I'm the comments with Chris the problem is unique –  Jul 16 '15 at 08:11
  • More unique or more general? And you have to say with your question how it differs from existing material, please don't bury this important information in some comments. – hakre Jul 17 '15 at 10:02

1 Answers1

1

You could do it this way.

$xml = new SimpleXMLElement('<product id="123"></product>');
if(!empty($xml['id'])) {
    $xml->addChild('id', $xml['id']);
    unset($xml['id']);
}
echo $xml->asXML();

Output:

<?xml version="1.0"?>
<product><id>123</id></product>

Here's the manual's link and the addchild functions link. http://php.net/manual/en/class.simplexmlelement.php
http://php.net/manual/en/simplexmlelement.addchild.php

Update:

If you had multiple products you could loop like this.

$xml = new SimpleXMLElement('<proudcts><product id="123"></product><product id="234"></product></proudcts>');
foreach($xml as $key => $data){
    if(!empty($data['id'])) {
        $data->addChild('id', $data['id']);
        unset($data['id']);
    }
}
echo $xml->asXML();

Output:

<?xml version="1.0"?>
<proudcts><product><id>123</id></product><product><id>234</id></product></proudcts>
chris85
  • 23,846
  • 7
  • 34
  • 51
  • Cool, but how do i set it up to loop through every product. Fix the product id then move to the other one? I updated the question too mate :) –  Jul 15 '15 at 23:52
  • Updated with an example of if you had multiple products. Your updated question looks like it only has one product still.. – chris85 Jul 16 '15 at 00:01
  • @Code any questions or issues? – chris85 Jul 16 '15 at 01:14
  • That's amazing thank you but I need to put the entire XML file as string into the sinplexmlelement? This is going to be an issue as the file that it need to iterate through is too large to just be loaded to memory. How can I iterate through a big file and apply this operation? –  Jul 16 '15 at 08:10
  • There are a ways to accomplish that, try these threads. http://stackoverflow.com/questions/911663/parsing-huge-xml-files-in-php and http://stackoverflow.com/questions/15363901/parsing-extremely-large-xml-files-in-php – chris85 Jul 16 '15 at 12:26