www.webdeveloper.com
Results 1 to 5 of 5

Thread: XML DOM: commit/save to xml file

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    XML DOM: commit/save to xml file

    Hi!

    I am implementing a XML database for my webpage. The idea is that the user will enter their information into a simple HTML form. My javascript will then parse, validate and then append the data as as new nodes to my XML file.

    I have done the parsing of the form by document.getElementByID().value (which works well), and loaded and edited my XML document with the *.appendChild() method. However, nothing happens to the XML file that was loaded.

    I occurred to me that maybe I had to 'commit' my changes, but I cannot find the correct method to do so on the w3c schools. I have found a micro$oft extention to the XML DOM that is called *.save() that I suppose would do the job. I am no fan of micro$oft however, and having made the rest of my site standards-compliant, it feels like crap having to use *one* MS extension.

    Bottom line: Is there a standards-compliant way of saving an XML document to a file on the server?

  2. #2
    Join Date
    Dec 2009
    Posts
    3

    Solved

    Problem solved!

    I crufted together something in PHP which did the job.

  3. #3
    Join Date
    Jan 2010
    Location
    Gloucestershire, UK
    Posts
    2

    Working on same project requirement

    You look like you were working on the same problem of committing the xml file that I've been working on. I wanted to POST the xmlDoc once I'd modified text nodes but have discovered you can't commit it the server without something in between.
    Could you share the PHP script you put together? Also the way you posted the xml file changes to the PHP script.
    Thanks.

  4. #4
    Join Date
    Dec 2009
    Posts
    3
    Sure. This is the script I threw together. I do not pretend to have any understanding of PHP, but it does what it was supposed to do. For the actual presentation of the database I have written a small piece of XSLT. You can find more information on www.w3schools.com.

    And, yeah; the identifiers are in Swedish. Sorry

    <?php
    $doc = new DOMDocument();
    $doc->formatOutput = true;
    $doc->load('rhelg.xml');
    $musiker = $doc->createElement ( "musiker" );

    $namn = $doc->createElement( "namn" );
    $namn->appendChild(
    $doc->createTextNode($_POST["namn"])
    );
    $musiker->appendChild( $namn );

    $instrument = $doc->createElement( "instrument" );
    $instrument->appendChild(
    $doc->createTextNode($_POST["instrument"])
    );
    $musiker->appendChild( $instrument );

    $status = $doc->createElement( "status" );
    $status->appendChild(
    $doc->createTextNode($_POST["status"])
    );
    $musiker->appendChild( $status);

    $kommentar = $doc->createElement( "kommentar" );
    $kommentar->appendChild(
    $doc->createTextNode($_POST["kommentar"])
    );
    $musiker->appendChild( $kommentar);

    $tid = $doc->createElement( "tid" );
    $tid->appendChild(
    $doc->createTextNode(strftime("%Y-%m-%d, %T") )
    );
    $musiker->appendChild( $tid );

    $r = $doc->documentElement;
    $r->appendChild( $musiker );

    echo $doc->save( "rhelg.xml" );
    ?>

  5. #5
    Join Date
    Jan 2010
    Location
    Gloucestershire, UK
    Posts
    2
    Thanks so much. That's really helpful.

    I build all my web sites using xml and xslt. I'm now extending things by gradually adding AJAX features.

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