Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 29th, 2003, 02:39 PM
Authorized User
Join Date: Oct 2003
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to replace elements(from existed XML file)?

I have a submit form with 2 submit button. All the submit buttons will post to ASP file to create an XML file. First submit button can only take 1 data and appending to root element. Meaning, it will overwrite the data if the button being clicked more than once. Second button, will append to the root element in XML file also but it can have many elements.


first element ---> <Name>Jessica</Name>

second elements ---> <Number><Phone>123</Phone></Number>
                ---> <Number><Phone>456</Phone></Number>

After created the XML file in ASP code, it will redirect to the form which will use data island to display the elements at the bottom of the form.

Eg: in form.html


    <xml id="name" src="nameXML.xml"></xml>

    <table border="0" cellpadding="1" datasrc="#name">

    <td><td><span datafld="Name"></span></td></td>


Name: Jessica



    <xml id="phone" src="nameXML.xml"></xml>

    <table border="0" cellpadding="1" datasrc="#phone">

    <td><td><span datafld="Phone"></span></td></td>


Phone: 123
Phone: 456


My problem is, after the Name element has been created and display on the form, if I enter Phone data and create the XML file, the Name element will be rewrite with nothing and display Phone element only. How can I retrieve the "Name" value from existed XML file and replace with the current blank - "Name" value if Name textbox is not being entered any data?? My ASP code as follow:-



Dim objDom
Dim objRoot
Dim objChild1
Dim objPI

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

'Load file
blnFileExists = objDom.Load(Server.MapPath("nameXML.xml"))

'Check if file exists
If blnFileExists = True Then

     'How to get the <Name> value from existed XML file and replace the current blank <Name> value (if the Name textbox has no data in name.html)
     'Set objRoot = objDom.documentElement

     'Create your root element and append it to the XML document.
    Set objRoot = objDom.createElement("root")
    objDom.appendChild objRoot

    Set mainChild = objDom.createElement("Name")
    mainChild.Text = Request.form("txtName")
    objRoot.appendChild mainChild

    'Create child element

     if Request.form("txtPhone") <> "" then
         Set objPhone = objDom.createElement("Phone")
         objPhone.Text = Request.form("txtPhone")
         objRoot.appendChild objPhone
     end if
    'Create the xml processing instruction.
    Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")

    'Append the processing instruction to the XML document.
    objDom.insertBefore objPI, objDom.childNodes(0)
End If

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

Set objRoot = Nothing
Set mainChild = Nothing
Set objChild1 = Nothing

'Redirect to name.html
Reply With Quote
  #2 (permalink)  
Old October 30th, 2003, 05:49 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

If I've understood, in your code, just before you create the name element you need to check whether or not the "txtName" has any content, if not do nothing with name element otherwise do as now.
The steps are:
1) Load doc
2) If successful go to step 4
3) Create doc with just <root/>
4) Select name node
5) If txtName has content replace content otherwise ignore
6) Add phone numbers

Steps 4 and 5 are done like this after loading doc into objDom
sName = Request("txtName")
Set oNameNode = objDom.selectSingleNode("/root/Name")
If Len(sName) > 0 Then
  oNameNode.text = sName
End If
Joe (MVP - xml)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace a text in XML file AyatKh XML 17 May 23rd, 2012 09:05 PM
XSL file can't find elements in XML data? Mateo1041 XSLT 2 September 18th, 2008 09:37 AM
Create XML file - add elements according to range remya1000 XML 5 October 3rd, 2007 02:50 PM
Get all elements from XML olster XSLT 0 February 13th, 2007 06:39 AM
XSL to replace text in XML file neetukk XSLT 1 November 29th, 2006 05:20 AM

All times are GMT -4. The time now is 12:26 PM.

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