Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Updating XML Child nodes using Classic ASP Problems

302 views
Skip to first unread message

abruton

unread,
Oct 30, 2009, 10:50:50 AM10/30/09
to
Hi

I am trying to update a child node of an XML file IE Changing the
value.

The XML file looks like this:

<user>
<firstname>Andre</firstname>
<lastname>Bruton</lastname>
</user>


Here is my Classic asp code:

users_firstname = "Tristan" 'New code to put in the XML file

Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load(cURL)
If xmlObj.parseError.errorCode <> 0 Then
Response.Write "Error Reaqding File - " & xmlObj.parseError.reason
& "<p>"
End If

Set xmlList = xmlObj.getElementsByTagName("user")
For Each xmlItem In xmlList
For Each xmlItem2 In xmlItem.childNodes
a = xmlItem2.nodeName
if a = "firstname" then firstname = xmlItem2.text
if a = "lastname" then lastname = xmlItem2.text
Next
Next

If firstname <> users_firstname Then
Set nodeBook = xmlObj.selectSingleNode("//firstname")
nodeBook.setAttribute "firstname", users_firstname
Response.Write nodeBook.getAttribute("firstname")
xmlObj.save(cDir & cFile)
End If

Set xmlObj = Nothing


The problem is that it adds a new section to the XML file instead of
updating the value of firstname from Andre to Tristan. The XML looks
like this:

<user>
<firstname firstname="Andre6">Andre</firstname>
<lastname>Bruton</lastname>
</user>


What is should look like is:

<user>
<firstname>Tristan</firstname>
<lastname>Bruton</lastname>
</user>


Any idea how I can fix this?

Best regards

Andre

Bob Barrows

unread,
Oct 30, 2009, 11:36:34 AM10/30/09
to

If you only have a single <user> element, there is no need to do a loop:
just use selectSingleNode:

set usernode = xmlObj.selectSingleNode("//user")

>
> If firstname <> users_firstname Then
> Set nodeBook = xmlObj.selectSingleNode("//firstname")
> nodeBook.setAttribute "firstname", users_firstname
> Response.Write nodeBook.getAttribute("firstname")
> xmlObj.save(cDir & cFile)
> End If
>
> Set xmlObj = Nothing
>
>
> The problem is that it adds a new section to the XML file instead of
> updating the value of firstname from Andre to Tristan. The XML looks
> like this:
>
> <user>
> <firstname firstname="Andre6">Andre</firstname>
> <lastname>Bruton</lastname>
> </user>
>
>
> What is should look like is:
>
> <user>
> <firstname>Tristan</firstname>
> <lastname>Bruton</lastname>
> </user>
>
>
> Any idea how I can fix this?
>

Don't use getAttribute or SetAttribute.
This is an attribute: firstname="Andre6"
This is an element: <firstname>Tristan</firstname>

Untested air code:
set fnamenode = xmlObj.selectSingleNode("//user/firstname")
if fnamenode.nodeValue <> users_firstname then
fnamenode.nodeValue = users_firstname
end if
Response.Write fnamenode.nodeValue
xmlObj.save cDir & cFile

--
HTH,
Bob Barrows


abruton

unread,
Oct 31, 2009, 12:52:10 AM10/31/09
to
Hi Bob

Thank you for the info. I managed to get it working late last night
using the following code...

If firstname <> users_firstname Then
Set objRoot = xmlObj.documentElement
Set objField = objRoot.selectSingleNode("firstname")
objField.Text = users_firstname


xmlObj.save(cDir & cFile)
End If

Best regards

Andre F Bruton

0 new messages