remove element from xml on php

Solutions on MaxInterview for remove element from xml on php by the best coders in the world

showing results for - "remove element from xml on php"
Lia
11 Apr 2017
1<?php
2
3$doc = new DOMDocument; 
4$doc->load('theFile.xml');
5
6$thedocument = $doc->documentElement;
7
8//this gives you a list of the messages
9$list = $thedocument->getElementsByTagName('message');
10
11//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
12$nodeToRemove = null;
13foreach ($list as $domElement){
14  $attrValue = $domElement->getAttribute('time');
15  if ($attrValue == 'VALUEYOUCAREABOUT') {
16    $nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
17  }
18}
19
20//Now remove it.
21if ($nodeToRemove != null)
22$thedocument->removeChild($nodeToRemove);
23
24echo $doc->saveXML(); 
25?>