Use ASP to create an XML file

Source: Internet
Author: User

<! -- # Include file = "conn. asp" -->
<%
Function LoadFile (byval file)
Dim objstream
On Error resume next
Set objstream = server. Createobject ("ADODB. Stream ")
If err. Number =-2147221005 then
Response. Write "<Div align = 'center'> sorry, your host does not support ADODB. Stream and cannot use this program </div>"
Err. Clear
Response. End
End if
With objstream
. Type = 1
. Open
. Loadfromfile server. mappath (file)
If err. Number <> 0 then
Response. Write "<Div align = 'center'> file <font color = '# ff0000'>" & file & "</font> cannot be opened. Check whether the file exists! </Font> </div>"
Err. Clear
Response. End
End if
LoadFile =. Read ()
. Close
End
Set objstream = nothing
End Function
%>
<%
'Create a DOM object
Set objdom = server. Createobject ("Microsoft. xmldom ")

'Retrieve XML data
'Method 1: Get the XML data of the XML file
'Objdom. Load ("C:/test. xml ")
'Method 2: Get the data of the XML data string
Objdom. loadxml ("<? XML version = "" 1.0 "" encoding = "" UTF-8 ""?> <Document/> ")

'Create a Node object
Set newnode = objdom. documentelement 'objdom. createelement ("document ")
Set newnodechild = objdom. createelement ("postdatetime ")
Newnodechild. Text = now
Newnode. appendchild newnodechild
Set newnodechild = objdom. createelement ("tablename ")
Newnodechild. Text = "Demo"
Newnode. appendchild newnodechild
Set newnodechild = objdom. createelement ("keyfield ")
Newnodechild. Text = "ID"
Newnode. appendchild newnodechild
Set newnodechild = objdom. createelement ("datatable ")
'Data table description
Set scnodechild = objdom. createelement ("field ")
Scnodechild. Text = "ID"
Set newattride = objdom. createnode ("attribute", "lable ","")
Newattribute. Text = "ID"
Scnodechild. setattributenode newattribute
Set newattride = objdom. createnode ("attribute", "datatype ","")
Newattribute. Text = "int"
Scnodechild. setattributenode newattribute
Newnodechild. appendchild scnodechild

Set scnodechild = objdom. createelement ("field ")
Scnodechild. Text = "title"
Set newattride = objdom. createnode ("attribute", "lable ","")
Newattribute. Text = "title"
Scnodechild. setattributenode newattribute
Set newattride = objdom. createnode ("attribute", "datatype ","")
Newattribute. Text = "varchar"
Scnodechild. setattributenode newattribute
Set newattride = objdom. createnode ("attribute", "len ","")
Newattribute. Text = "200"
Scnodechild. setattributenode newattribute

Set scnodechild = objdom. createelement ("field ")
Scnodechild. Text = "content"
Set newattride = objdom. createnode ("attribute", "lable ","")
Newattribute. Text = "content"
Scnodechild. setattributenode newattribute
Set newattride = objdom. createnode ("attribute", "datatype ","")
Newattribute. Text = "text"
Scnodechild. setattributenode newattribute
Newnodechild. appendchild scnodechild

Set scnodechild = objdom. createelement ("field ")
Scnodechild. Text = "postdatetime"
Set newattride = objdom. createnode ("attribute", "lable ","")
Newattribute. Text = "Release Date"
Scnodechild. setattributenode newattribute
Set newattride = objdom. createnode ("attribute", "datatype ","")
Newattribute. Text = "datetime"
Scnodechild. setattributenode newattribute
Newnodechild. appendchild scnodechild

Set scnodechild = objdom. createelement ("field ")
Scnodechild. Text = "AddFile"
Set newattride = objdom. createnode ("attribute", "lable ","")
Newattribute. Text = "attachment"
Scnodechild. setattributenode newattribute
Set newattride = objdom. createnode ("attribute", "datatype ","")
Newattribute. Text = "text"
Scnodechild. setattributenode newattribute
Newnodechild. appendchild scnodechild

Newnode. appendchild newnodechild
'End data table description
'Add data description
Set newnodechild = objdom. createelement ("add ")
'End add data description
SQL = "select * from demo where status = 'add '"
Rs. Open SQL, Conn, 1, 3
Do while not Rs. EOF
Set scnodechild = objdom. createelement ("list ")
Set trnodechild = objdom. createelement ("ID ")
Trnodechild. Text = RS ("ID ")
Scnodechild. appendchild trnodechild
Set trnodechild = objdom. createelement ("title ")
Trnodechild. Text = server. htmlencode (RS ("title "))
Scnodechild. appendchild trnodechild
Set trnodechild = objdom. createelement ("content ")
Trnodechild. Text = server. htmlencode (RS ("content "))
Scnodechild. appendchild trnodechild
Set trnodechild = objdom. createelement ("postdatetime ")
Trnodechild. Text = RS ("postdatetime ")
Scnodechild. appendchild trnodechild
Set trnodechild = objdom. createelement ("AddFile ")
If RS ("AddFile") <> "" then
Set newattride = objdom. createnode ("attribute", "filetype ","")
Newattribute. Text = RS ("filetype ")
Trnodechild. setattributenode newattribute
Trnodechild. datatype = "bin. base64"
Trnodechild. nodetypedvalue = LoadFile (RS ("AddFile "))
End if
Scnodechild. appendchild trnodechild
Newnodechild. appendchild scnodechild
Rs. movenext
Loop
Rs. Close
Newnode. appendchild newnodechild
'Modify data
Set newnodechild = objdom. createelement ("edit ")
SQL = "select * from demo where status = 'modify '"
Rs. Open SQL, Conn, 1, 3
Do while not Rs. EOF
Set scnodechild = objdom. createelement ("list ")
Set trnodechild = objdom. createelement ("keyValue ")
Trnodechild. Text = RS ("ID ")
Scnodechild. appendchild trnodechild
Set fournodechild = objdom. createelement ("field ")
Set newattride = objdom. createnode ("attribute", "fieldname ","")
Newattribute. Text = "title"
Fournodechild. setattributenode newattribute
Fournodechild. Text = server. htmlencode (RS ("title "))
Scnodechild. appendchild fournodechild
Set fournodechild = objdom. createelement ("field ")
Set newattride = objdom. createnode ("attribute", "fieldname ","")
Newattribute. Text = "content"
Fournodechild. setattributenode newattribute
Fournodechild. Text = server. htmlencode (RS ("content "))
Scnodechild. appendchild fournodechild
Set fournodechild = objdom. createelement ("field ")
Set newattride = objdom. createnode ("attribute", "fieldname ","")
Newattribute. Text = "AddFile"
Fournodechild. setattributenode newattribute
If RS ("AddFile") <> "" then
Set newattride = objdom. createnode ("attribute", "filetype ","")
Newattribute. Text = RS ("filetype ")
Fournodechild. setattributenode newattribute
Fournodechild. datatype = "bin. base64"
Fournodechild. nodetypedvalue = LoadFile (RS ("AddFile "))
End if
Scnodechild. appendchild fournodechild
Newnodechild. appendchild scnodechild
Rs. movenext
Loop
Rs. Close
Newnode. appendchild newnodechild
'End modify data
'Delete data
Set newnodechild = objdom. createelement ("delete ")
Set scnodechild = objdom. createelement ("list ")
SQL = "select * from demo where status = 'delete '"
Rs. Open SQL, Conn, 1, 3
Do while not Rs. EOF
Set trnodechild = objdom. createelement ("keyValue ")
Trnodechild. Text = RS ("ID ")
Scnodechild. appendchild trnodechild
Rs. movenext
Loop
Rs. Close
Newnodechild. appendchild scnodechild
Newnode. appendchild newnodechild
'End: delete data.
'Objdom. appendchild newnode
Objdom. Save (server. mappath ("demo. xml "))
Connclose
Response. contenttype = "application/XML"
Response. Write (objdom. XML)
'Response. Redirect ("demo. xml ")

%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.