As you can exactly say which elements you want to be removed, this is normally done easiest with xpath by querying these elements and then removing them.
In SimpleXML:
$remove = '//junk'; // all <junk> tags anywhere
// simplexml
$sx = simplexml_load_string($xml);
foreach ($sx->xpath($remove) as $element) {
unset($element->{0});
}
In DOMDocument:
$remove = '//junk'; // all <junk> tags anywhere
// dom
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->query($remove) as $element) {
$element->parentNode->removeChild($element);
}
Full Example (Demo):
<?php
/**
* @link http://stackoverflow.com/a/26318711/367456
* @link https://eval.in/204702
*/
$xml = <<<BUFFER
<aa>
<bb>Some text goes here and <br /> some more on a new line
there are other <junk/> tags that I want to keep ignoring
</bb>
</aa>
BUFFER;
$remove = '//junk'; // all <junk> tags anywhere
// simplexml
$sx = simplexml_load_string($xml);
foreach ($sx->xpath($remove) as $element) {
unset($element->{0});
}
$sx->asXML('php://output');
// dom
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
foreach ($xpath->query($remove) as $element) {
$element->parentNode->removeChild($element);
}
$doc->save('php://output');
Output:
<?xml version="1.0"?>
<aa>
<bb>Some text goes here and <br/> some more on a new line
there are other tags that I want to keep ignoring
</bb>
</aa>
<?xml version="1.0"?>
<aa>
<bb>Some text goes here and <br/> some more on a new line
there are other tags that I want to keep ignoring
</bb>
</aa>
some more on a new line there are other tags that I want to keep ignoring" – David Oct 01 '14 at 22:47
");` Now, all that it will keep is the `
` tags....try it, you will see it will work. Whatever is inside the quotes it will keep and strip any other tag. Believe me!! – Rasclatt Oct 01 '14 at 22:50