Write/Append XML

T

thomas

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>
 
A

Andy Dingley

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top