1

Why is my code not functioning to delete an element located in my asset.xml

Here is my xml code inside a php file:

<?php

if(isset($_POST["delete"])) {
        $node = $_GET["node"]; //get from form
        $xmldoc->load('asset.xml');
        $y= $xmldoc->getElementsByTagName("asset")[$node];
        $xmldoc.documentElement.removeChild($y);}
?>

my xml file

<?xml version="1.0" encoding="UTF-8"?>
<Assets>
  <asset>
    <AssetType>PROJECTOR</AssetType>
    <Product>DELL</Product>
    <Brand>DELL</Brand>
  </asset>
</Assets>
5
  • 5
    $xmldoc.documentElement.removeChild($y); - What's that?! Please do us all a favour and paste code, not type from memory. Commented Dec 19, 2012 at 6:48
  • i already paste it.. the code suppose to become like what? Commented Dec 19, 2012 at 7:53
  • If this is your code, PHP would give you very obvious signs that something is wrong; have you tried to run the code? What are the errors that you see? Commented Dec 19, 2012 at 7:57
  • i'm using the Uniform Server as my local server. There are no error displayed. However, each time I click the button (name = 'delete'), the page become a totally blank page Commented Dec 19, 2012 at 8:07
  • possible duplicate of A simple program to CRUD node and node values of xml file Commented Dec 19, 2012 at 8:48

2 Answers 2

1

you need to first save file try

$xmldoc->save('asset.xml');

and

The removeChild() method removes a specified node.
The removeAttribute() method removes a specified attribute.

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

1 Comment

i want to remove the <asset></asset>, there are a few <asset></asset> inside the xml file.
1

You'll have to save the file for the changes to persist

$xmldoc->save('asset.xml');

Seeing as the code you posted is actual code

DOMDocument::getElementsByTagName returns a DOMNodeList you'll have to access the elements via DOMNodelist::item

$y = $xmldoc->getElementsByTagName("asset")->item($node);//assuming $node is an integer < # of matched nodes

-> is used to access object properties in php not . so $xmldoc.documentElement.removeChild($y); should be

$xmldoc->documentElement->removeChild($y);

or better yet

$y->parentNode->removeChild($y);

1 Comment

I already did the update, the result is, I got a totally blank page.

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.