How to change XML tag names using PHP?

I have an XML file that looks something like this:

<product> <modelNumber>Data</modelNumber> <salePrice>Data</salePrice> </product> <product> <modelNumber>Data</modelNumber> <salePrice>Data</salePrice> </product> 

Is there an easy way to change tag names, something else, for example, model, price.

In fact, I have a bunch of XML files containing similar data, but in different formats, so I'm looking for an easy way to parse an XML file, change certain tag names and write a new XML file with the modified tags.

+8
xml php
source share
3 answers

The following function will do the trick:

 /** * @param $xml string Your XML * @param $old string Name of the old tag * @param $new string Name of the new tag * @return string New XML */ function renameTags($xml, $old, $new) { $dom = new DOMDocument(); $dom->loadXML($xml); $nodes = $dom->getElementsByTagName($old); $toRemove = array(); foreach ($nodes as $node) { $newNode = $dom->createElement($new); foreach ($node->attributes as $attribute) { $newNode->setAttribute($attribute->name, $attribute->value); } foreach ($node->childNodes as $child) { $newNode->appendChild($node->removeChild($child)); } $node->parentNode->appendChild($newNode); $toRemove[] = $node; } foreach ($toRemove as $node) { $node->parentNode->removeChild($node); } return $dom->saveXML(); } // Load XML from file data.xml $xml = file_get_contents('data.xml'); $xml = renameTags($xml, 'modelNumber', 'number'); $xml = renameTags($xml, 'salePrice', 'price'); echo '<pre>'; print_r(htmlspecialchars($xml)); echo '</pre>'; 
+6
source share

There are two problems with the Kris and dfsq code:

  • Only the first child node is copied - solved using a temporary copy of $ childNodes)
  • Children will get the xmlns tag - solved by replacing the node at the beginning - therefore it is associated with the document

Corrected rename function:

 function renameTag( DOMElement $oldTag, $newTagName ) { $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); $oldTag->parentNode->replaceChild($newTag, $oldTag); foreach ($oldTag->attributes as $attribute) { $newTag->setAttribute($attribute->name, $attribute->value); } foreach (iterator_to_array($oldTag->childNodes) as $child) { $newTag->appendChild($oldTag->removeChild($child)); } return $newTag; } 
+8
source share

Here is an example of the code that works in my question here , but there is no direct way to change the tag name via DOMDocument / DOMElement, however you can copy the elements with the new tag as shown.

basically you should:

 function renameTag(DOMElement $oldTag, $newTagName) { $document = $oldTag->ownerDocument; $newTag = $document->createElement($newTagName); foreach($oldTag->attributes as $attribute) { $newTag->setAttribute($attribute->name, $attribute->value); } foreach($oldTag->childNodes as $child) { $newTag->appendChild($oldTag->removeChild($child)); } $oldTag->parentNode->replaceChild($newTag, $oldTag); return $newTag; } 
+1
source share

All Articles