Richard Muthwill
Richard Muthwill

Reputation: 336

PHP XML Remove Parent

XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
    <pages>
      <page><title>Home</title><content>Lorem Ipsum</content></page>
      <page><title>Pictures</title><content>Lorem Ipsum</content></page>
      <page><title>Information</title><content>Lorem Ipsum</content></page>
    </pages>
  <css>
    <css-tag><title>background-color</title><value>#FFF</value></css-tag>
  </css>
  <layout>1</layout>
</root>

PHP:

$title = $_GET['0'];

$xml = new DOMDocument('1.0', 'ISO-8859-1');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = true;
$xml->load($location);
$pages = $xml->getElementsByTagName("page");
foreach($pages as $page){
    $pagetitle = $page->getElementsByTagName("title");
    $pagetitlevalue = $pagetitle->item(0)->nodeValue;
    if($title == $pagetitlevalue){
        $pagetitle->item(0)->parentNode->removeChild($pagetitle->item(0));
    }
}
$xml->save($location);

This code gets rid of just the <Title> node, how can it be changed to get rid of the parent <Page> node?

I can't figure out how to do this, I just manage to get rid of the title node and get loads of error codes

Upvotes: 2

Views: 2110

Answers (1)

Scuzzy
Scuzzy

Reputation: 12332

Whilst this is something I'd probably use xpath for, you can find...

$pagetitle->item(0)->parentNode->removeChild($pagetitle->item(0));

and replace with...

$pagetitle->item(0)->parentNode->parentNode->removeChild($pagetitle->item(0)->parentNode);

to go one level higher in your XML tree

Upvotes: 2

Related Questions