Write/Append XML

Discussion in 'XML' started by thomas, May 17, 2005.

  1. thomas

    thomas Guest

    I have the below form and I want the contents of the fields to be passed to
    an ASP file which then writes them to an XML file. I've used a version of
    this before, I then changed it but now it doesn't work. Any ideas?


    new_user.html
    ==========
    <html>
    <head>
    <title>New User</title>
    <link href="mystyle.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <form action="new_user.asp" method="post" name="new_user_form">
    <table class = "one">
    <tr>
    <td>
    <table class = "one">
    <tr>
    <td>Username:</td>
    <td><input name="user" id="user" type="text" ></td>
    </tr>
    <tr>
    <td>Password:</td>
    <td><input name="pass" id="pass" type="text"></td>
    </tr>
    </table>
    <p><input type="submit" name="Submit" value="Confirm Details"></p>
    <p><a href="index.html">Return to Login Page</a></p>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>


    new_user.asp
    =========
    <%@ Language="VBScript" %>

    <%

    dim objDom, fileExists, objFieldValue, objPI, nodes, path

    Set objDom = server.CreateObject("Microsoft.XMLDOM")
    objDom.preserveWhiteSpace = True


    fileExists=objDom.Load(Server.MapPath("users.xml"))
    If fileExists = True Then
    Set objRoot = objDom.documentElement
    Else
    Set objRoot = objDom.createElement("members")
    objDom.appendChild objRoot
    End If


    Set objRecord = objDom.createElement("member")
    objRoot.appendChild objRecord

    Set objFieldValue = objDom.createElement("username")
    objFieldValue.Text = Request.Form("user")
    objRecord.appendChild objFieldValue

    Set objFieldValue = objDom.createElement("password")
    objFieldValue.Text = Request.Form("pass")
    objRecord.appendChild objFieldValue


    set xmlDocument=CreateObject("MSXML2.FreeThreadedDOMDocument")
    xmlDocument.async="false"
    xmlDocument.load(Server.MapPath("users.xml"))


    path = "members"
    set nodes = xmlDocument.selectNodes(path)
    If nodes.length = 0 Then


    If fileExists = False Then
    Set objPI=objDom.createProcessingInstruction("xml", "version='1.0'")
    objDom.insertBefore objPI, objDom.childNodes(0)
    End If
    Else
    Response.Redirect("error.html")
    End If


    objDom.save(Server.MapPath("users.xml"))
    Response.Redirect("success.html")

    %>

    %>


    users.xml
    ======
    <?xml version="1.0"?>
    <members>
    <member>
    <username>thomas</username>
    <password>123456</password>
    </member>
    <member>
    <username>martin</username>
    <password>111111</password>
    </member>
    <member>
    <username>stuart</username>
    <password>654321</password>
    </member>
    </members>
    thomas, May 17, 2005
    #1
    1. Advertising

  2. thomas

    Andy Dingley Guest

    On Mon, 16 May 2005 23:40:25 GMT, "thomas" <>
    wrote:

    >I have the below form and I want the contents of the fields to be passed to
    >an ASP file which then writes them to an XML file. I've used a version of
    >this before, I then changed it but now it doesn't work. Any ideas?


    Diff it against the one that worked ?

    (Oh, and you're being very free and easy with user-supplied data values.
    Never trust a user.)

    Really though I'd bin the whole page. Use a database, not XML. There's
    a whole pile of multi-user locking problems you need to think about here
    and your big monolithic XML file is just ignoring them all. This isn't
    code that's anything like "web-ready" and it's more trouble to make it
    so than it would be to just switch to a database.

    XML isn't a database. It works with "documents", units of work that
    roughly represent one transaction. The database analogy is the row of a
    view (or maybe a view), not a table and certainly not a whole database.
    You're performing a task here which will pretty much never need to
    access the entire user list, just a single user - yet choosing XML
    forces you to load, edit and save the whole set every time. That's just
    a bad idea and fixing it (although possible) is harder than it would be
    to do it by an easier method in the first place.

    You never _have_ to use XML.


    --
    Cats have nine lives, which is why they rarely post to Usenet.
    Andy Dingley, May 17, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HALLES
    Replies:
    5
    Views:
    326
    HALLES
    May 31, 2005
  2. HYRY
    Replies:
    10
    Views:
    582
    Bruno Desthuilliers
    Sep 26, 2007
  3. write to existing file append

    , Aug 22, 2005, in forum: ASP General
    Replies:
    2
    Views:
    192
  4. Benjamin Trendelkamp-Schroer

    append() vs. write()

    Benjamin Trendelkamp-Schroer, Jan 10, 2012, in forum: Java
    Replies:
    7
    Views:
    637
  5. Iulian Ilea
    Replies:
    1
    Views:
    290
    pcx99
    Dec 21, 2006
Loading...

Share This Page