Click to See Complete Forum and Search --> : Help on childnodes() issues


redice
06-17-2009, 03:03 AM
Hi all,

I have been assigned to solve this asp/xml page issue, which was not written by me. The developer left, so I am left with no clue.

Since I have zero background about xml and have spent 3 days searching for answers in forums and google, here I am.

Here is the code. Based on the remarks written, it's obvious it's trying to save new nodes on top of the tree by doing some copy and replacement, which I am really confused about.




<%
dim intID, id,title,details,date1
dim msg
dim SupervisorName
dim objXML, objXMLData, xmlcontents, xmlcontent, xmlcontentPre
set objXML = server.CreateObject("Microsoft.FreeThreadedXMLDOM")
set objXMLData = server.CreateObject("Microsoft.FreeThreadedXMLDOM")
SupervisorName = Request.QueryString("SupervisorName")
id = "Auto"

if request.Form("Submit") <> "" then
objXML.async = False
'check if the file exists
blnFileExist = objXML.load(xmlpath)
if request.QueryString("id") = "" then
' if it doesn't create it, if it does then get the node count
' take note of IntID, thats the way we get the count of nodes
If blnFileExist = False Then
objXML.appendChild(objXML.createProcessingInstruction("xml","version=""1.0"""))
intID = 1
Else
objXML.Load(xmlpath)
objXMLData.Load(xmlpath)
set xmlcontents = objXMLData.DocumentElement.selectSingleNode("contents")
if objXML.childnodes(0).childnodes(0).hasChildNodes then
intID = xmlcontents.Childnodes.length + 1
else
intID = 1
end if
End If

' creates an element called note and under it elements that
' hold the data we want it to.
Set objXMLTop = objXML.createElement("Content")
objXMLTop.appendChild(objXML.createElement("ID"))
objXMLTop.appendChild(objXML.createElement("title"))
objXMLTop.appendChild(objXML.createElement("details"))
objXMLTop.appendChild(objXML.createElement("date"))
objXMLTop.appendChild(objXML.createElement("editor"))

Set objXMLv = objXML.createElement("Content")
objXMLv.appendChild(objXML.createElement("ID"))
objXMLv.appendChild(objXML.createElement("title"))
objXMLv.appendChild(objXML.createElement("details"))
objXMLv.appendChild(objXML.createElement("date"))
objXMLv.appendChild(objXML.createElement("editor"))

for i = xmlcontents.ChildNodes.length to 1 Step -1
set xmlcontent = xmlcontents.ChildNodes.item(xmlcontents.ChildNodes.length - 1)
set xmlcontentPre = xmlcontents.ChildNodes.item(i - 1)
if i = xmlcontents.ChildNodes.length - 1 then
'Copy the last content at the bottom to the newly appended node
objXMLTop.childNodes(0).text = xmlcontent.ChildNodes.item(0).text
objXMLTop.childNodes(1).text = xmlcontent.ChildNodes.item(1).text
objXMLTop.childNodes(2).text = xmlcontent.ChildNodes.item(2).text
objXMLTop.childNodes(3).text = xmlcontent.ChildNodes.item(3).text
objXMLTop.childNodes(4).text = xmlcontent.ChildNodes.item(4).text

Set objXMLv = objXML.createElement("Content")
objXMLv.appendChild(objXML.createElement("ID"))
objXMLv.appendChild(objXML.createElement("title"))
objXMLv.appendChild(objXML.createElement("details"))
objXMLv.appendChild(objXML.createElement("date"))
objXMLv.appendChild(objXML.createElement("editor"))

objXMLv.childNodes(0).text = xmlcontentPre.ChildNodes.item(0).text
objXMLv.childNodes(1).text = xmlcontentPre.ChildNodes.item(1).text
objXMLv.childNodes(2).text = xmlcontentPre.ChildNodes.item(2).text
objXMLv.childNodes(3).text = xmlcontentPre.ChildNodes.item(3).text
objXMLv.childNodes(4).text = xmlcontentPre.ChildNodes.item(4).text

Set objXMLvOld = objXML.childnodes(0).childnodes(0).childnodes(i)
objXML.documentElement.childnodes(i).replaceChild objXMLv, objXMLvOld
objXML.save(xmlpath)


else
' Copy the rest of the data so that the new node will appear at the top of the tree
Set objXMLv = objXML.createElement("Content")
objXMLv.appendChild(objXML.createElement("ID"))
objXMLv.appendChild(objXML.createElement("title"))
objXMLv.appendChild(objXML.createElement("details"))
objXMLv.appendChild(objXML.createElement("date"))
objXMLv.appendChild(objXML.createElement("editor"))

objXMLv.childNodes(0).text = xmlcontentPre.ChildNodes.item(0).text
objXMLv.childNodes(1).text = xmlcontentPre.ChildNodes.item(1).text
objXMLv.childNodes(2).text = xmlcontentPre.ChildNodes.item(2).text
objXMLv.childNodes(3).text = xmlcontentPre.ChildNodes.item(3).text
objXMLv.childNodes(4).text = xmlcontentPre.ChildNodes.item(4).text

set objXMLvOld = objXML.childnodes(0).childnodes(0).childnodes(i)
objXML.documentElement.childnodes(0).replaceChild objXMLv, objXMLvOld
objXML.save(xmlpath)
end if
next
' Insert new item to the top
Set objXMLv = objXML.createElement("Content")
objXMLv.appendChild(objXML.createElement("ID"))
objXMLv.appendChild(objXML.createElement("title"))
objXMLv.appendChild(objXML.createElement("details"))
objXMLv.appendChild(objXML.createElement("date"))
objXMLv.appendChild(objXML.createElement("editor"))

objXMLv.childNodes(0).text = intID
objXMLv.childNodes(1).text = request.form("title")
objXMLv.childNodes(2).text = request.form("details")
objXMLv.childNodes(3).text = request.form("itemDate")
objXMLv.childNodes(4).text = SupervisorName

set objXMLvOld = objXML.childnodes(0).childnodes(0).childnodes(0)
objXML.documentElement.childnodes(0).replaceChild objXMLv, objXMLvOld
objXML.save(xmlpath)
' Append new added note with copied item below
objXML.documentElement.childnodes(0).appendChild(objXMLTop.cloneNode(true))
objXML.save(xmlpath)
else
Set objXMLv = objXML.createElement("Content")
objXMLv.appendChild(objXML.createElement("ID"))
objXMLv.appendChild(objXML.createElement("title"))
objXMLv.appendChild(objXML.createElement("details"))
objXMLv.appendChild(objXML.createElement("date"))
objXMLv.appendChild(objXML.createElement("editor"))

objXMLv.childNodes(0).text = request.form("id")
objXMLv.childNodes(1).text = request.form("title")
objXMLv.childNodes(2).text = request.form("details")
objXMLv.childNodes(3).text = request.form("itemDate")
objXMLv.childNodes(4).text = request.form("SupervisorName")
set objXMLvOld = objXML.childnodes(0).childnodes(0).childnodes(request.QueryString("id"))
objXML.documentElement.childnodes(0).replaceChild objXMLv, objXMLvOld
objXML.save(xmlpath)
end if

'good idea to do, saves mem
Set objXMLv = Nothing
Set objXML = Nothing
msg = "Item is saved!"
response.Redirect("adminlistcontents.asp?SupervisorName=" + SupervisorName)
end if

if request.QueryString("id") <> "" then
objXML.Load(xmlpath)
set xmlcontents = objXML.DocumentElement.selectSingleNode("contents")
set xmlcontent = xmlcontents.ChildNodes.item(request.QueryString("id"))
id = xmlcontent.ChildNodes.item(0).text
title = xmlcontent.ChildNodes.item(1).text
details = xmlcontent.ChildNodes.item(2).text
date1 = xmlcontent.ChildNodes.item(3).text
' The Supervisor login to modify is shown as the editor.
end if

%>








xml code

<aspxml>
<contents>
<Content>
<ID>246</ID>
<title>Title A</title>
<details>This is a good title</details>
<date>11/6/2009</date>
<editor>Jamie C</editor>
</Content>
<Content>
<ID>245</ID>
<title>Title B</title>
<details>Another good title</details>
<date>11/6/2009</date>
<editor>Jamie C</editor>
</Content>
</contents>
</aspxml>




I found another page written by the same programmer, it can add successfully without any error, but the new entry is at the bottom of the xml, instead of the top.

With the above example, if a new entry is to be added, ID 247 will be before 246.

I hope I have made myself clear. Right now, the error message I get is:

Microsoft VBScript runtime error '800a01a8'
Object required: 'childnodes(...).childnodes(...)'
/online/OnlineTeamBriefing/newcontent.asp, line 29


Looking forward to hearing from you. Thank you.

Kor
06-17-2009, 05:52 AM
<Content>
<ID>246</ID>
...
</Content>

to avoid any problem better use lowercase for all your elements(tags) and their attributes.

jkmyoung
06-19-2009, 03:34 PM
If blnFileExist = False then do you ever initialize xmlcontents?

redice
06-21-2009, 10:19 PM
If blnFileExist = False then do you ever initialize xmlcontents?

Hi jkmyoung,

I am not too sure about this. Perhaps you could guide me here? The thing that bothers me is that it worked previously with the same code. Right now, suddenly, it just didn't.

Could it be the xml version or anything I might have missed?

Thanks.

jkmyoung
06-22-2009, 10:43 AM
In your if clause where it asks if binFileExist = False, I would consider initializing xmlcontents to an empty <contents> node.

Either that or change the line
for i = xmlcontents.ChildNodes.length to 1 Step -1
to take into account that xmlcontents may not have been initialized.

redice
07-01-2009, 03:15 AM
Hi jkmyong,

I tried your suggestion and now I have this error:


Microsoft VBScript runtime error '800a01a8'
Object required: ''


Help??

mrpals
07-01-2009, 04:38 AM
thansk for any answer I will try.
mrpals.com (http://www.mrpals.com)

jkmyoung
07-07-2009, 02:29 PM
Line number? Code at that line?

redice
07-10-2009, 03:22 AM
Hi jkmyoung,

line 55 : for i = xmlcontents.ChildNodes.length - 1 to 1 Step -1

Thank you.

jkmyoung
07-10-2009, 12:09 PM
To be very clear, when you say you took my suggestion, then when binFileExist = False, you set xmlContents to equal a new empty NodeList?

Could you post the code where you do so?