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>
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
user1914937
  • 27
  • 1
  • 2
  • 5
    `$xmldoc.documentElement.removeChild($y);` - What's that?! Please do us all a favour and *paste* code, not type from memory. – Ja͢ck Dec 19 '12 at 06:48
  • i already paste it.. the code suppose to become like what? – user1914937 Dec 19 '12 at 07: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? – Ja͢ck Dec 19 '12 at 07: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 – user1914937 Dec 19 '12 at 08:07
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Dec 19 '12 at 08:48

2 Answers2

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);
Musa
  • 96,336
  • 17
  • 118
  • 137
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.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143