curious if this is possible, im currently creating an XML database driven forum, i was just curious if its possible to remove/edit an xml entry by using its ID to delete it

IE:

<id>1</id>
<author>Jay</author>
<content>bla bla bla</content>

where as id like to remove the post with id 1 🙂.
thanx
50G

    It is possible, yes. But that isn't an XML file.

      it was an example lol...

      ill be more XMLish 🙂:

      <?xml version="1.0" encoding="ISO-8859-1" ?>
      <post>
      <id>1</id>
      <author>Jay</author>
      <content>Reminder</content>
      </post>
      <post>
      <id>2</id>
      <author>Jay</author>
      <content>Reminder 2</content>
      </post>

      how wud i delete post with id 1 in php?

        That's still not an XML document. Try opening it in a browser and see.

        But if it had been an XML document, I would (this isn't shortwave radio) have used DOM and XPath. I like XPath. It's like an unholy cross between a filesystem and SQL.

        $doc = new DOMDocument;
        $doc->load('file.xml');
        $xpath =  new DOMXpath($doc);
        $nodeToKill = $xpath->query('//id[text()="1"]/..')->item(0);
        $nodeToKill->parentNode->removeChild($nodeToKill);
        $doc->save('newfile.xml');
        

        The XPath finds all <id> elements in the document, restricted to those that have a text node child with a value of "1", then finds their parents.

        Then it just picks the first one in the resulting list to be the one to remove.
        The line after that removes it from the document.

          Write a Reply...