9

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 a simple way to change the tag names , to something else such as model, price.

Essentially, I have a bunch of XML files containing similar data, but in different formats, so I'm looking for a simple way to parse the XML file, change certain tag names, and write a new XML file with the changed tag names.

5
  • Is your XML intentionally malformed?
    – kapa
    Commented Nov 17, 2011 at 7:17
  • The files are provides by multiple sources -- the tag names are uncontrolled by me.
    – Ryan
    Commented Nov 17, 2011 at 7:19
  • So errors like <salePrice>Data</modelNumber> (different ending/closing tag) are normal? Because then you won't really be able to parse this document with an XML parser (like PHP DOM).
    – kapa
    Commented Nov 17, 2011 at 7:20
  • No. That was a bug on my part when typing it. I just read it over.
    – Ryan
    Commented Nov 17, 2011 at 7:22
  • Related: stackoverflow.com/a/21885789/175071 Commented Mar 11, 2014 at 13:17

3 Answers 3

8

There are two issues with Kris and dfsq code:

  • Only first child node will be copied - solved with temporary copy of $childNodes)
  • Children will get xmlns tag - solved by replacing node at the beginning - so it's connected to the document

A corrected renaming function is:

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;
}
6

Next 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>';
2
  • Where exactly do I load the original XML file?
    – Ryan
    Commented Nov 17, 2011 at 18:58
  • Write into file (easiest way) with file_put_contents('filename.xml', $xml);
    – dfsq
    Commented Nov 18, 2011 at 11:50
1

There is some sample code that works in my question over here, but there is no direct way of changing a tag name through DOMDocument/DOMElement, you can however copy elements with a new tagname as shown.

basically you have to:

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;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.