Use ASP to generate XML data documents

Source: Internet
Author: User

1. You must find out what is ultimately needed

Through ASP or other dynamic programming languages, we finally need data in XML format, which is irrelevant to the file carrier where the XML data is located, it can be a real XML file, such as: http://www.dw8.cn/common/dw8.xml. Can also be for ASP documents, such as: http://www.cnbruce.com/blog/rss2.asp

They are the embodiment of XML data. To achieve the dynamics of XML data, we need to use dynamic programming languages, such as ASP to generate it.

Ii. How to generate dynamic XML documents

If an XML file is generated, the dynamic file is in ASP format. Therefore, you must use FSO to generate an XML file, for example:

The following is a reference clip:
<%
Xmlfile = server. mappath ("test1.xml ")
Set FSO = Createobject ("scripting. FileSystemObject ")
Set myfile = FSO. createtextfile (xmlfile, true)
Myfile. writeline ("<? XML version = "" 1.0 "" encoding = "" gb2312 ""?> ")
Myfile. writeline ("<world> ")
Myfile. writeline ("<Hello> Hello, world </Hello> ")
Myfile. writeline ("</World> ")
Myfile. Close
%>

<A href = "test1.xml"> View XML file content </a>

For more information about FSO operations, see
Http://www.cnbruce.com/blog/showlog.aspcat_id=26&log_id=440

If the dynamic XML data file is generated, the name and value of the XML node can be controlled through procedural means in the dynamic document based on the content of myfile. writeline.

3. How to use dynamic documents to generate XML data

If the XML file is not generated and XML data is output directly in the dynamic file, the file type (response. contenttype) must be declared)

<% Response. contenttype = "text/XML" %>

For example, you can directly browse the following dynamic ASP document and display it as an XML data tree in the browser.

The following is a reference clip:
<%
With response
. Contenttype = "text/XML"
. Write ("<? XML version = "" 1.0 "" encoding = "" gb2312 ""?> ")
. Write ("<world> ")
. Write ("<Hello> Hello, world </Hello> ")
. Write ("</World> ")
End
%>

The advantage of the generated XML file is that the file that processes the XML data can be a static file. For example, the HTML file parses the XML through JavaScript and xmldom, and the data is easy to retain, the dynamic XML data on dynamic documents is not like this. However, in today's era of ubiquitous use of dynamic documents, it seems that this advantage has little impact on some applications. In addition, the XML data stream of dynamic documents is even more advantageous: more timely and dynamic.

 

4. Is it true to generate XML data?

Whether generating a specific XML file or a dynamic XML data stream, you only need to output the relevant XML nodes and values according to the XML format. In this case, XML seems very simple. However, this does not really involve XML operations. In our opinion, these XML is nothing more than a pair of tags and data records composed of relevant characters, no vitality at all. However, XML operations through xmldom show the absolute advantages of XML (this is not obvious when generating XML, but it is infinite when adding or deleting XML nodes ).

Use xmldom to create an XML document. You can use the Save method to generate an XML document. Use the createelement method to create XML elements and createnode to create nodes, in fact, you can select either of the tags in XML. However, you can use createelement to create top-level (Root) elements and createnode to create subnodes (elements ), of course, the use of createelement and createnode is also different.

 

 

The following is a reference clip:
<%
Set objxmldoc = Createobject ("Microsoft. xmldom ")
Set world = objxmldoc. createelement ("world ")
Objxmldoc. appendchild (World)
Set Hello = objxmldoc. createnode ("element", "hello ","")
Hello. Text = "Hello, world"
Objxmldoc.doc umentelement. appendchild (Hello)
Objxmldoc. Save server. mappath ("test2.xml ")
Set objxmldoc = nothing
%>

 

 

  1. Createobject ("Microsoft. xmldom") declares the use of xmldom objects
  2. When an element or node is created (createelement or createnode), it is not added to the file tree. To add a node to the file tree, you need to insert it, such as appendchild.
  3. Xmldocument. createnode (type, name, namespaceuri) indicates creating a new node of the specified type, name, and namespace.
    Type is used to confirm the node type to be created. Name is a string to confirm the name of the new node. The namespace prefix is optional. Namespaceuri is a string that defines the namespace URI. If the prefix is included in the name parameter, this node is created with the specified prefix in the namespaceuri document. If the prefix is not included, the specified namespace is considered as the preset namespace.
    Objxmldoc. createnode ("element", "hello", "") is equivalent to objxmldoc. createelement ("hello ")
  4. 4. objxmldoc.doc umentelement. appendchild (Hello) is actually a node created under the root element of the XML document. In this example, it is equivalent to world. appendchild (Hello). World is the node name in this example, and so on.

So you can write it like this:

 

The following is a reference clip:
<%
Set objxmldoc = Createobject ("Microsoft. xmldom ")
Set world = objxmldoc. createelement ("world ")
Objxmldoc. appendchild (World)
Set Hello = objxmldoc. createelement ("hello ")
Hello. Text = "Hello, world"
World. appendchild (Hello)
Objxmldoc. Save server. mappath ("test2.xml ")
Set objxmldoc = nothing
%>

 

It should be noted that the XML files generated through xmldom are in UTF-8 format, which is a good introduction to the UTF-8 of all our application files.

Summary

To generate XML data, you can use FSO. For example, if FSO is disabled, you can use xmldom. Of course, you can also directly use dynamic documents. However, xmldom operations are required if you have a thorough understanding of XML operations.

<%
'------------------------------------------------------
'Read the file readtxtfile (filename)
'------------------------------------------------------
Function readtxtfile (filename)
Dim FSO, F1, ts, filepath
Filepath = server. mappath (filename)
Set FSO = Createobject ("scripting. FileSystemObject ")
Set Ts = FSO. opentextfile (filepath, 1, 1)
Readtxtfile = ts. readall
Set Ts = nothing
Set FSO = nothing
End Function
'------------------------------------------------------------
'Write information into the file
'------------------------------------------------------------
Function writetxtfile (text, filename)
Path = server. mappath (filename)
Set FSO = Createobject ("scripting. FileSystemObject ")
Set F1 = FSO. createtextfile (path, true)
F1.write (text)
F1.close
End Function

<%
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open applicatio ("connstring ")
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * from Table order by ID DESC"
Rs. Open SQL, Conn
Do while not Rs. EOF
MSG = MSG & RS ("Field 1") & CHR (9) & RS ("Field 2) & vbcrlf
Rs. movenext
Loop

Call writetxtfile (MSG, "xmllover.xls ")
%>

 

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.