2

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

2
  • Can you edit your question with a minor change so I can undo my accidental down vote? Commented Apr 26, 2012 at 22:18
  • 1
    @Scuzzy Been a while but there you go haha Commented Apr 3, 2016 at 23:58

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.