0

I have a xml file with servers in it.

<?xml version="1.0" encoding="UTF-8"?>
<servers>
   <server>
      <location>Belgium</location>
      <ip>192.168.0.1</ip>
      <port>8080</port>
      <crt></crt>
   </server>
   <server>
      <location>Fhoto</location>
      <ip>192.168.0.5</ip>
      <port>7841</port>
      <crt>http://127.0.0.1/serv.crt</crt>
   </server>
   <server>
      <location>TestingPanel</location>
      <ip>192.168.1.2</ip>
      <port>6969</port>
      <crt>http://testingpanel.com/server.crt</crt>
   </server>
   <server>
      <location>TestingPanel2</location>
      <ip>192.168.1.3</ip>
      <port>6968</port>
      <crt>http://testingpanel.com/server1.crt</crt>
   </server>
</servers>

I made a php page that can add servers to the file and it works great. Now I want to make a form where you can enter the IP and the php script should delete the whole node.

So for example. I have a form with a textbox and I enter "192.168.0.1". I want it to delete this:

   <server>
      <location>Belgium</location>
      <ip>192.168.0.1</ip>
      <port>8080</port>
      <crt></crt>
   </server>

I have this so far (basically nothing):

if (isset($_POST["deleteServer"])) { 
    //do processing
}

Please guide me in the right direction as I have no idea how to do this. Thanks in advance!

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Niell
  • 77
  • 12
  • ^ it actually works like with the attribute, with the element, just remove the `@` commercial-at-sign in front of it, then xpath recognizes it as the element (instead of the attribute). - See especially the technique outlined in this answer: http://stackoverflow.com/a/16062633/367456 – hakre Dec 28 '13 at 18:29

2 Answers2

2

I recommend using DomDocument and Xpath for finding the node:

$xml = new DOMDocument();
$xml->load($filePath);
$xpathXml = new DOMXPath($xml);

Next thing u need to do is to find node:

$elements = $xpathXml->query("//Server[ip="$yourIp"]);

And remove the elements that have been found from parent node:

foreach ($elements as $element) {
  $element->parentNode->removeChild($element);
}
1

Load the file using simplexml_load_file(), loop through the <server> nodes and check if the IP is equal to the given IP.

If it is, use unset() on the self-reference of the SimpleXMLElement node to remove it:

$xml = simplexml_load_file('file.xml');
foreach ($xml as $key => $server) {
    if ( $server->ip != '192.168.0.1') {
        continue;
    }
    unset($server[0]);
}

Demo.

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • You can also fire-up an xpath query with the condition. – hakre Dec 28 '13 at 18:34
  • @hakre: The current version emits a warning. What was wrong with original answer? – Amal Murali Dec 28 '13 at 18:36
  • Yes, that warning requires a break: https://eval.in/private/38933ffe246242 (considered it's one element to delete) or just the suggested xpath. What was wrong? Nothing wrong, just some superfluous doings, like casting to string while PHP does that for you or not using unset but instead calling conversion functions and other objects methods. – hakre Dec 28 '13 at 18:41
  • This is perhaps more nice: https://eval.in/83809 – hakre Dec 28 '13 at 18:46