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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML + ASP Append

Status
Not open for further replies.

dranium

Technical User
Mar 9, 2009
15
CA
Is it possible to pick which XML node to write to? Right now the script is currently writing into the <RSS> node. I want everything to write in the <contact> node. So the final result would be <rss><contact><name>My Name</name></contact></rss>

Here is what it currently does.

Code:
Contact.XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="/css/rss_itemcontent.css" type="text/css" media="screen"?>
<rss xmlns:content="[URL unfurl="true"]http://purl.org/rss/1.0/modules/content/"[/URL] xml:lang="fr-CA" version="2.0">
<contact></contact>
<item>
<title>My Fname</title>
<pubDate>Lname</pubDate>
<link>444 Street</link>
<description>444 Stree2</description>
<author>343-394-3049</author>
<category>jdk@email.ca</category>
</item>
</rss>

XML.asp
<%
 'Declare local variables.
 Dim objDom
 Dim objRoot
 Dim objRecord
 Dim objField
 Dim objFieldValue
 Dim objattID
 Dim objattTabOrder
 Dim objPI
 Dim blnFileExists
 Dim x

 'Instantiate the Microsoft XMLDOM.
 Set objDom = server.CreateObject("Microsoft.XMLDOM")
 objDom.preserveWhiteSpace = False
 blnFileExists = objDom.Load(Server.MapPath("Contact.xml"))
 response.write Server.MapPath("Contact.xml")

 'Test to see if the file loaded successfully.
 If blnFileExists = True Then
	Set objRoot = objDom.documentElement
 Else
 'Create your root element and append it to the XML document.
  Set objRoot = objDom.createElement("channel")
  objDom.appendChild objRoot
 End If

   Dim name, phone, address, newperson 
   name = Request("firstName") 
   lname = Request("lastName") 
   address = Request("address1") 
   address2 = Request("address2")
   phone = Request("phone")
   email = Request("email")

 Dim root 
   Set root = objDom.documentElement 
   Set newperson = objDom.createNode("element", "item", "") 

   Dim newname, newphone, newaddress, newdescription, newauteur, newcategory
   
   Set newname = objDom.createNode("element", "title", "") 
   newname.text = name  
   
   Set newphone = objDom.createNode("element", "pubDate", "")
   newphone.text = lname
   
   Set newaddress = objDom.createNode("element", "link", "")
   newaddress.text = address
   
   Set newdescription = objDom.createNode("element", "description", "")
   newdescription.text = address2
   
   Set newauteur = objDom.createNode("element", "author", "")
   newauteur.text = phone
   
   Set newcategory = objDom.createNode("element", "category", "")
   newcategory.text = email

   newperson.appendChild(newname)
   newperson.appendChild(newphone) 
   newperson.appendChild(newaddress)
   newperson.appendChild(newdescription)
   newperson.appendChild(newauteur)
   newperson.appendChild(newcategory)

   objDom.documentElement.appendChild(newperson.cloneNode(True))

 'Check once again to see if the file loaded successfully. If it did
 'not, that means we are creating a new document and need to be sure to
 'insert the XML processing instruction.
 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
 Set objPI = objDom.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'")
 objDom.insertBefore objPI, objDom.childNodes(0)
 objDom.save(Server.MapPath("Contact.xml")) 

 '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
%>

Thank you!
 
The final result would be...

Code:
<rss>
<contact>

<item>
<name>1st person</name>
</item>

<item>
<name>2nd person</name>
</item>

</contact>
</rss>
 
What posted is not sufficiently cleanup, hence, lot of errors contradictory to what said results. Hence is a minial re-work. You need further do all the clean up. It is not very neat, consistent in notion and contains redundancy.
[tt]
<%
'Declare local variables.
Dim objDom
Dim objRoot
Dim objRecord
Dim objField
Dim objFieldValue
Dim objattID
Dim objattTabOrder
Dim objPI
Dim blnFileExists
Dim x

'Instantiate the Microsoft XMLDOM.
Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = False
blnFileExists = objDom.Load(Server.MapPath("Contact.xml"))
response.write Server.MapPath("Contact.xml")

'Test to see if the file loaded successfully.
If blnFileExists = True Then
Set objRoot = objDom.documentElement
Else
'Create your root element and append it to the XML document.
[red]'[/red]Set objRoot = objDom.createElement("channel")
[blue]Set objRoot = objDom.createElement("rss")[/blue]
[blue]objRoot.setAttribute "xmlns:content", "[ignore][/ignore]"
objRoot.setAttribute "xml:lang", "fr-CA"[/blue]
objDom.appendChild objRoot
[blue]objRoot.appendChild(objDom.createNode("element", "contact", ""))[/blue]
End If

Dim name, phone, address, newperson
name = Request("firstName")
lname = Request("lastName")
address = Request("address1")
address2 = Request("address2")
phone = Request("phone")
email = Request("email")

Dim root
Set root = objDom.documentElement[blue].getElementsByTagName("contact")(0) 'in fact the contact node[/blue]
Set newperson = objDom.createNode("element", "item", "")

Dim newname, newphone, newaddress, newdescription, newauteur, newcategory

Set newname = objDom.createNode("element", "title", "")
newname.text = name

Set newphone = objDom.createNode("element", "pubDate", "")
newphone.text = lname

Set newaddress = objDom.createNode("element", "link", "")
newaddress.text = address

Set newdescription = objDom.createNode("element", "description", "")
newdescription.text = address2

Set newauteur = objDom.createNode("element", "author", "")
newauteur.text = phone

Set newcategory = objDom.createNode("element", "category", "")
newcategory.text = email

newperson.appendChild(newname)
newperson.appendChild(newphone)
newperson.appendChild(newaddress)
newperson.appendChild(newdescription)
newperson.appendChild(newauteur)
newperson.appendChild(newcategory)

[red]'[/red]objDom.documentElement.appendChild(newperson.cloneNode(True))
[blue]root.appendChild newperson[/blue]

'Check once again to see if the file loaded successfully. If it did
'not, that means we are creating a new document and need to be sure to
'insert the XML processing instruction.
If blnFileExists = False then

'Create the xml processing instruction.
'Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
[blue]Set objPI = objDom.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'")[/blue]

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

'Save the XML document.
'objDom.save strXMLFilePath & "\" & strFileName
[red]'[/red]Set objPI = objDom.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'")
[red]'[/red]objDom.insertBefore objPI, objDom.childNodes(0)
objDom.save(Server.MapPath("Contact.xml"))

'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
%>
[/tt]
 
This works great! Thank you for the help tsuji.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top