Quote:
Originally Posted by Tex2002ans
You would then use this library to go through the XML (perhaps convert it into an array of strings), and from there, step through the array and make any changes as needed.
|
I would avoid converting to/from strings because of all the fun that entities cause when doing so. Leave the DOM objects as a tree and just manipulate the objects to change the tag names, add or remove attributes, etc. But otherwise, yes.
For example, in PHP, you can do something like this:
Code:
<?php
require('phpQuery/phpQuery.php'); // JQuery port to PHP
$doc = new DOMDocument();
if (!$doc->loadXML(file_get_contents("/path/to/file.xml")) {
// handle error
exit(1);
}
/* Useful function */
function changeElementTagName($elt, $newTagName)
{
$newelt = $Document->createElement(newTagName);
// Clone the element's attributes
foreach($elt->attributes as $attribute) {
$newelt->setAttribute($attribute->name, $attribute->value);
}
// Clone the element's content
foreach($elt->childNodes as $child) {
$newelt->appendChild($child->cloneNode(true));
}
// Replace the node in the tree
$elt->parentNode->replaceChild($newelt, $elt);
}
foreach (pq("title") as $titleelt) {
changeElementTagName($titleelt, "span");
$titleelt->setAttribute("class", "title");
}
$outputstring = $doc->saveXML();
print $outputstring;
?>
Or whatever. (Note: I have not actually tested this code, and it may not even parse correctly.)