1

I want to delete particular node in XML based on the id I passed in the xml. The below seems not working please help me.

$id  = $_GET['nodeId'];
$dom = new DOMDocument;
$dom->load('Seat_matrix.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/location/cubicle[./id = "%d"]', $id);
foreach($xpath->query($query) as $cubicle) {
    $record->parentNode->removeChild($cubicle);
}
$dom->save("Seat_matrix.xml");

XML file

<location>

    <cubicle>
        <id> 6121</id>
        <status>2</status>
        <shift1>  
            <Employee1>Tom</Employee1>
        </shift1>           
        <shift2>  
            <Employee2>arum</Employee2>
        </shift2>
    </cubicle>
</location>
2
  • @Gordon, don't post answers as comments. Post that as an answer, because if that provides author with valid answer, author will not be able to accept a comment. Commented Jul 13, 2011 at 14:39
  • Hi I am getting Fatal error: Call to a member function removeChild() on a non-object on this line :$record->parentNode->removeChild($cubicle); Commented Jul 13, 2011 at 14:48

1 Answer 1

1

You are using sprintf with %d but the element node contains " 6121" (mind the space in the beginning).

var_dump(sprintf('%d', ' 6121') === ' 6121'); // FALSE

Use %s instead. Also. there is no $record variable, so you have to change the code to

$cubicle->parentNode->removeChild($cubicle);
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.