• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Find and remove child from XML with PHP

LeX

ლ(╹◡╹ლ)
NLC
I have an XML document:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<level1>
  <data fruit="apple"/>
  <data fruit="orange"/>
  <data fruit="pear"/>
</level1>
I want to find the <data> with attribute 'fruit' that is an "orange", and delete the whole <data> so that the resulting XML looks like
Code:
<?xml version="1.0" encoding="UTF-8"?>
<level1>
  <data fruit="apple"/>
  <data fruit="pear"/>
</level1>
How do I do this with PHP? I'm going nuts...
Here's what I currently have, but it's not working:
Code:
$doc = simplexml_load_file("file.xml");
foreach($doc->data as $data) {
	if($data['fruit'] == 'orange') {
	$dom=dom_import_simplexml($data);
	$dom->parentNode->removeChild($dom);
	}
}
Any ideas? Thanks.
 
Thanks for your reply, but none of them addresses my problem. The first one I've visited already, actually, but it only edits the textnode, the "apple" in a <data>apple</data> syntax.

The second one is similar in that it searches the textnode, not the element attribute.
 
I finally got it to work, I'll post the code for future reference.

Code:
$xdoc = new DOMDocument();
$xdoc->load('file.xml');
$level = $xdoc->getElementsByTagName('level1')->item(0);
$fruits = $level->getElementsByTagName('data');
foreach ($fruits as $fruit) {
	if (strpos($fruit->getAttribute('fruit'), 'orange') !== false) {
		$xdoc->documentElement->removeChild($fruit);
		}
	}
$xdoc->save('file.xml');
 
Hi, I'm glad to find this thread as I'm trying to do something basically identical but I'm not able to get it to work and I wonder if someone here might be able to help me spot the problem. I have an XML file with google maps markers stored as follows:

<marker lat="XXXX" lng="YYYYY" comment="ZZZZZ"/><marker .... />...

I'm trying to remove the marker whose lat value matches the value of a JavaScript variable (markerlatitude).

Right now I have the following function in JavaScript (where init() just reinitializes the map):

Code:
function deleteMarker() {
	var markerlatitude = document.getElementById("markerlatitude").value;

	var getVar =  "?markerlatitude=" + markerlatitude;

	var request = GXmlHttp.create();
	
	//open the request to deleteMarker.php on server
	request.open('GET', 'deleteMarker.php' + getVar, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			init();
		}
	}
	request.send(null);
	return false;
}

And the following deleteMarker.php file, adapted from the post above:

Code:
<?php
$markerlatitude = (float)$_GET['markerlatitude'];

$xdoc = new DOMDocument();
$xdoc->load('data.xml');
$lats = $xdoc->getElementsByTagName('marker')->item(0);
foreach ($lats as $lat) {
	if (strpos($lat->getAttribute('lat'), $markerlatitude) !== false) {
		$xdoc->documentElement->removeChild($lat);
		}
	}
$xdoc->save('data.xml');
?>

Any ideas? Thanks very much in advance.

Eric
 
Back
Top