Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML prepend Child

Status
Not open for further replies.

kingpin123

Technical User
Nov 23, 2006
1
CA
Hell all
The reason why I am writing is because I am having trouble with appending information into my XML file and I thought that maybe someone could help me out.

What I would like is to have the new information inserted at the top of my XML file and not at the bottom. I was reading about prependChild but I am not getting it to work. Can you help me out.

This a function that I call in my asp code that appends information into an XML file.

<%
addContact "c:\projects\xml\xml","contact.xml"
If err.number <> 0 then Response.write("Errors occurred while saving your form submission.") Else Response.write("Your form submission has been saved.")

Function addContact(strXMLFilePath, strFileName)
Dim objDom
Dim objRoot
Dim objRecord
Dim objField
Dim objFieldValue
Dim objattID
Dim objattTabOrder
Dim objPI
Dim blnFileExists

Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = True
blnFileExists = objDom.Load(strXMLFilePath & "\" & strFileName)
If blnFileExists = True Then
Set objRoot = objDom.documentElement
Else
Set objRoot = objDom.createElement("users")
objDom.appendChild objRoot
End If

'Create the new container element for the new record.
Set objRecord = objDom.createElement("profile")
objRoot.appendChild objRecord 'I think that over here I should write objRoot.prependChild obijRecord. But I am getting an error

For x = 1 To Request.Form.Count
If instr(1,Request.Form.Key(x),"btn") = 0 Then
'Create an element, "field".
Set objField = objDom.createElement(Request.Form.Key(x))

'Create a new element, "field_value".
Set objFieldValue = objDom.createElement(Request.Form.Key(x))

'Set the value of the field_value element equal to the value of the current field in the Form Collection.
objFieldValue.Text = Request.Form(x)

'Append the field element as a child of the new record container element, contact.
objRecord.appendChild objField

'Append the field_value element as a child of the field element.
objField.appendChild objFieldValue
End If
Next

If blnFileExists = False then '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

'Save the XML document.
objDom.save strXMLFilePath & "\" & strFileName

'Release all of your object references.
Set objDom = Nothing
Set objRoot = Nothing
Set objRecord = Nothing
Set objField = Nothing
Set objFieldValue = Nothing
Set objattID = Nothing
Set objattTabOrder = Nothing
Set objPI = Nothing
End Function
%>


Thank-you indvance
 
I'm not sure why you double up field and fieldvalue nodes, and for some reason call the field "contact". But if the positioning of objRecord is the problem, you can always do this.
>objRoot.appendChild objRecord
[tt]objRoot.insertbefore objRecord, objRoot.firstchild [/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top