p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XML (http://p2p.wrox.com/forumdisplay.php?f=91)
-   -   How to replace elements(from existed XML file)? (http://p2p.wrox.com/showthread.php?t=5538)

hbcontract October 29th, 2003 02:39 PM

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.

Eg:

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

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

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
===================
....

<h2>Name</h2>

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

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

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

    </table>
==========================
Output:

Name: Jessica

=========================
....

<h2>Phone</h2>

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

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

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

    </table>
==================
Output:

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
        
Else


     '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
response.redirect("name.html")
%>

joefawcett October 30th, 2003 05:49 AM

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
Code:

sName = Request("txtName")
Set oNameNode = objDom.selectSingleNode("/root/Name")
If Len(sName) > 0 Then
  oNameNode.text = sName
End If

Joe (MVP - xml)


All times are GMT -4. The time now is 05:38 AM.

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