save xml problem

Discussion in 'JavaScript' started by omerta, Jan 2, 2008.

  1. #1
    i have this script:

    <script type="text/javascript">

    var xmlDoc=loadXMLDoc("login.xml");

    var x=xmlDoc.getElementsByTagName('name');
    for (i=0;i<x.length;i++)
    {
    if( x.childNodes[0].nodeValue=='<?php echo $_POST["name"]; ?>' )
    {
    location="eggrafi_error5.html";
    }
    }

    x=xmlDoc.documentElement;

    var name = xmlDoc.createElement('name','<?php echo $_POST["name"]; ?>');
    var password = xmlDoc.createElement('password','<?php echo $_POST["password"]; ?>');


    var new_user=xmlDoc.createElement('users');


    new_user.appendChild(name);
    new_user.appendChild(password);

    x.appendChild(new_user);

    xmlDoc.save("login.xml");

    </script>

    the login.xml is like that:
    <login><users>
    <name>george</name>
    <password>123456</password>
    </name>
    </login>

    my problem is that the only command that doesn;t work is the last one: xmlDoc.save("login.xml");

    i can't save the xml file....

    is sth wrong?

    the function loadXMLDoc is the above:

    function loadXMLDoc(dname)
    {
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject)
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    xmlDoc.async=false;
    xmlDoc.load(dname);
    return(xmlDoc);
    }
     
    omerta, Jan 2, 2008 IP
  2. omerta

    omerta Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    does anyone can help?

    how can i save my xml file with javascript??

    pls help...
     
    omerta, Jan 3, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Where did you get the save function from?
     
    MMJ, Jan 3, 2008 IP
  4. omerta

    omerta Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i saw a script from an exercise but it must be in php script...
    i thought it would be the same in javascript.
    generally how can i save my xml file in javascript?
    Can i manipulate my xml file with php?
    i tried to do that in php but i was confused with load and save functions.
    any good tutorial about the above? any easy script for example?
    i use this link for tutorial: http://w3schools.com/
     
    omerta, Jan 4, 2008 IP
  5. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    MMJ, Jan 4, 2008 IP
  6. omerta

    omerta Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    look at this script:

    <?php
    $xmlDoc = new DomDocument();
    $xmlDoc->load('exhibitions.xml');
    //Returns a list of elements with the given tag name:'EXS'.
    $exhibitions = $xmlDoc -> getElementsByTagName('EXS');

    //Creates new elements with the given tag names.
    $name = $xmlDoc->createElement('NAME', $_POST["name"]);
    $type = $xmlDoc->createElement('TYPE', $_POST["type"]);
    $subj = $xmlDoc->createElement('SUBJ', $_POST["subject"]);
    $place = $xmlDoc->createElement('PLACE', $_POST["place"]);
    $date = $xmlDoc->createElement('DATE', $_POST["date"]);
    $price = $xmlDoc->createElement('PRICE', $_POST["price"]);
    $artists = $xmlDoc->createElement('ARTISTS', $_POST["artists"]);
    $descr = $xmlDoc->createElement('DESCR', $_POST["description"]);

    $newExh=$xmlDoc->createElement('EXH');

    //Add a node to the end of the list of children of the parent node "newExh".
    $newExh->appendChild($name);
    $newExh->appendChild($type);
    $newExh->appendChild($subj);
    $newExh->appendChild($place);
    $newExh->appendChild($date);
    $newExh->appendChild($price);
    $newExh->appendChild($artists);
    $newExh->appendChild($descr);

    //Add a node to the end of the list of children of the parent node "exhibitions".
    $exhibitions->item(0)->appendChild($newExh);

    $xmlDoc -> save("exhibitions.xml");
    ?>

    this is asp?
    it works fine anyway.

    in javascript how can i save?
    in your example
    $myxmlstuff = '<?xml ... ?>'; is like load?

    sorry for too much questions....
     
    omerta, Jan 4, 2008 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No, thats PHP.

    You can't do file I/O in javascript.

    No, thats just a variable that has some example xml code.
     
    MMJ, Jan 4, 2008 IP
  8. omerta

    omerta Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yes but this function:
    file_put_contents('file.xml', $myxmlstuff);

    isn't I/O function?
     
    omerta, Jan 5, 2008 IP
  9. omerta

    omerta Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    and sth else

    $myxmlstuff = '<?xml ... ?>';

    is there any function who loads and save from an xml file and not from a variable, in php of course?
     
    omerta, Jan 5, 2008 IP
  10. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #10
    MMJ, Jan 5, 2008 IP