Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 27th, 2003, 03:00 PM
Authorized User
 
Join Date: Oct 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Help in Updating XML file.

Hi all, I tried to update my XML file but it didn't success. I have a form written in ASP. When user click on submit button, it will check if the specified XML file is exists. If it's not, it will then parse the form value and create an xml file (I did this part successfully). But if the user click on the submit button again, it should get the new values 'append' in XML file. It didn't success. Eg:

First time submit button clicked:
=============================
<?xml version="1.0"?>
<contact>
    <Name>First</Name>
    <Age>1</Age>
</contact>

Second time submit button clicked:
=============================
<?xml version="1.0"?>
<contact>
    <Name>First</Name>
    <Age>1</Age>
    <Name>Second</Name>
    <Age>2</Age>
</contact>

Below is my code: "a.asp"
========================

<html>
<body>

<form action="a.asp" method="get">
Your name: <input type="text" id="fname" name="fname" size="20"><br>
Age: <input type="text" id="age" name="age" size="20">
<input type="submit" id="btnSub" name="btnSub" value="Submit">
</form>

<%

 Dim objDom
 Dim objRoot
 Dim objChild1
 Dim objChild2
 Dim objPI

 Set objDom = Server.CreateObject("Microsoft.XMLDOM")

 set fs=Server.CreateObject("Scripting.FileSystemObject ")

 if fs.FileExists("c:\Inetpub\wwwroot\contact.xml")=fa lse then
     response.write("File Not exists <br><br>")

 Set objRoot = objDom.createElement("contact")

 objDom.appendChild objRoot

 Set objChild1 = objDom.createElement("Name")
 objChild1.Text = Request.queryString("fname")
 objRoot.appendChild objChild1

 Set objChild2 = objDom.createElement("Age")
 objChild2.Text = Request.queryString("age")
 objRoot.appendChild objChild2

 Set objPI = objDom.createProcessingInstruction("xml","version= '1.0'")

 objDom.insertBefore objPI, objDom.childNodes(0)

 objDom.Save "c:\Inetpub\wwwroot\contact.xml"

 else
     'This part is to append the nodes to existing XML file.

     response.write("File Exists <br><br>")

 ' set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
 ' xmlDoc.async="false"
 ' xmlDoc.load("contact.xml")

 ' set newnode=xmldoc.createElement("Name")
 ' newnode.Text = Request.queryString("fname")
 ' xmldoc.appendchild(newnode)

 ' xmlDoc.Save "c:\Inetpub\wwwroot\contact.xml"

 end if

 set fs=nothing

%>

<xml id="contact" src="contact.xml"></xml>

NAME: <span DATASRC="#contact" datafld="Name"></span>&nbsp;&nbsp;&nbsp;AGE: <span DATASRC="#contact" datafld="Age"></span><br><br>

</body>
</html>




 
Old October 28th, 2003, 05:40 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

"Microsoft.XmlDom" is severely deprecated as you don't know what dom class you'll get. If you can control the environment use install version 4.0 sp2 from msdn and use "Msxml2.DomDocument.4.0" of failing that most servers will have version 3, "Msxml2.DomDocument.3.0".

Also you don't need to bother with the filesystemobject, just try to load the file, if it's not there the load will return false.

That aside I'm assuming you want to create the file if necessary but if it is there already add a new Name/Age pair. I think your code should be:
Code:
 Dim objDom
 Dim objRoot
 Dim objChild1
 Dim objChild2
 Dim bSuccess
 Dim sPath 

 sPath = Server.MapPath("/contact.xml")) 
 Set objDom = CreateObject("Msxml2.DomDocument.3.0") 'See note above
 objDom.async = False
 bSuccess = objDom.load(sPath)
 If bSuccess = False Then
   objDom.loadXML "<contact/>"
 End If 
 Set objRoot = objDom.documentElement
 Set objChild1 = objDom.createElement("Name")
 objChild1.text = Request.queryString("fname")
 objRoot.appendChild objChild1
 Set objChild2 = objDom.createElement("Age")
 objChild1.text = Request.queryString("age")
 objRoot.appendChild objChild2
 objDom.save sPath
Joe (MVP - xml)
 
Old October 28th, 2003, 04:34 PM
Authorized User
 
Join Date: Oct 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Joe :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on Processing two xml and updating one a sayanand XSLT 7 October 12th, 2007 05:35 AM
Updating an XML Schema johnnycorpse ASP.NET 1.0 and 1.1 Professional 0 April 5th, 2007 11:18 AM
updating an xml file Yustme C# 0 June 1st, 2006 02:58 PM
updating xml files vwrg VB How-To 0 January 25th, 2005 08:20 PM
Updating an excel file via PHP Annoyamouse PHP How-To 0 December 23rd, 2004 07:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.