www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] Removing particular XML elements with SimpleXML

  1. #1
    Join Date
    Jan 2010
    Posts
    39

    resolved [RESOLVED] Removing particular XML elements with SimpleXML

    Hello everyone.

    I am trying to edit an element on a XML file with SimpleXML, and as far as I am concerned I have to remove the old one and then add the new one. First of all, my XML file is like this:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Document>
    <Placemark id="1000"><!-- children elements --></Placemark>
    <Placemark id="1001"><!-- children elements --></Placemark>
    <Placemark id="1002"><!-- children elements --></Placemark>
    ...
    </Document>
    I load the XML like this:
    PHP Code:
    $xmlfile'universities.xml';
    $xml simplexml_load_file($xmlfile) or die("Αρχείο XML δε βρέθηκε");
    $institutions=$xml->children(); 
    Now i find the <Placemark> element with the id attribute I want like this:
    PHP Code:
    foreach ($institutions as $institution)  {
    if (
    $institution['id']==$id) {
    $current_uni $institution;
    }

    What do I have to do to remove this particular element?

    Thank you in advanced.

  2. #2
    Join Date
    Jan 2010
    Posts
    39
    No worries, got it. Found out that I had to use DOM, simpleXML does not remove stuff.

    Just for anyone interested:

    PHP Code:
    $id $_POST["id"];

    foreach(
    $xml->Placemark as $uni)
    {
        if(
    $uni['id'] == $id) {
            
    $dom=dom_import_simplexml($uni);
            
    $dom->parentNode->removeChild($dom);
        }
    }
    if ((
    $xml->asXml($xmlfile)) == 1)
        echo 
    'All good';
    else
        echo 
    'Woopsie, try again'

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles