-1

My XML file is given below I'm using the xml as external file:

<?xml version="1.0"?>
<custscales>
    <custscale sclNo="1" type="lin">
        <scaleName>Custom Scale Lin</scaleName>
        <jsfunc>custLin</jsfunc>
    </custscale>
    <custscale sclNo="2" type="map">
        <scaleName>Custome Scale Map</scaleName>
        <jsfunc>custMap</jsfunc>
    </custscale>
    <custscale sclNo="3" type="pol">
        <scaleName>Custome Scale Pol</scaleName>
        <jsfunc>custPol</jsfunc>
    </custscale>
    <custscale sclNo="4" type="tbl1">
        <scaleName>Custome Scale Table</scaleName>
        <jsfunc>custTbl1</jsfunc>
    </custscale>
</custscales>

From the above xml file I just want to delete the node where sclNo ="4" I mean the node bellow should not be in file after save.

    <custscale sclNo="4" type="tbl1">
        <scaleName>Custome Scale Table</scaleName>
        <jsfunc>custTbl1</jsfunc>
    </custscale>

It is requested to give the example using simpleXML.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sonnet
  • 79
  • 1
  • 2
  • 7
  • @Rolando-Isidoro would you please answer my question? – Sonnet Apr 29 '13 at 23:42
  • Into which concrete problem are you running? Where is your code where you run into the error? What specifically does not work? Where do you hit the rock? Any error messages? Which of the existing solutions here on this website did not work for you? And writing *"Thanks in advance"* is often considered rude by other users, better not do that. – M8R-1jmw5r Apr 30 '13 at 02:51
  • -1 for not providing own code and rude feedback to people putting in time and effort to answer. – michi May 01 '13 at 16:55

3 Answers3

2
<?php

$doc = new DOMDocument; 
$doc->load('theFile.xml');

$thedocument = $doc->documentElement;

$list = $thedocument->getElementsByTagName('custscale ');


$nodeToRemove = null;
foreach ($list as $domElement){
  $attrValue = $domElement->getAttribute('sclNo');
  if ($attrValue == '4') {
    $nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
  }
}

if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);

echo $doc->saveXML(); 
?>

As taken fom here

Community
  • 1
  • 1
aran
  • 10,978
  • 5
  • 39
  • 69
  • the exact code I post may not work on your program. But trust me, that's how you delete specific nodes ; ). Take a look at the link provided. – aran May 01 '13 at 17:55
0

using simplexml:

$xml = simplexml_load_string($x); // assume XML in $x
$i = count($xml); 

for ($i; $i > 0; $i--) {   
    $cs = $xml->custscale[$i];
    if ($cs['sclNo'] == "4") unset($xml->custscale[$i]);
}
michi
  • 6,565
  • 4
  • 33
  • 56
  • @Sonnet: of course it is working. you have an error somewhere. post your code in your original question, makes it a lot easier than just posting "not working". – michi May 01 '13 at 16:41
0

It is requested to give the example using simpleXML

This has been outlined in detail in Remove a child with a specific attribute, in SimpleXML for PHP.

The key point is that you assign the element you want to unset to a variable - e.g. $element - for example by querying the single or multiple nodes with an Xpath query (standard example) - and then unsetting it:

$element = $xml->xpath('/path/to/element/to[@delete = "true"]')[0];
unset($element[0]);

That is already it in simplexml. As you can see, this is really simple. A full example:

$xml = simplexml_load_string(<<<BUFFER
<path>
 <to>
   <element>
     <to delete="true" />
   </element>
 </to>
</path>
BUFFER;
);

$element = $xml->xpath('/path/to/element/to[@delete = "true"]')[0];
unset($element[0]);

$xml->asXML('php://output');

See it in action.

Community
  • 1
  • 1
M8R-1jmw5r
  • 4,896
  • 2
  • 18
  • 26