How to use ASP to generate XML data documents (RSS feeds)

Source: Internet
Author: User
Tags format end reference string version advantage
rss|xml| Generate xml| Data

One must find out what is needed in the end

Through ASP or other dynamic programming languages, we ultimately need XML-formatted data, which has nothing to do with the file carrier of the XML data, it can be a real XML file, such as: HTTP://WWW.XRSS.CN/WEBRSS.ASP. It can also be an ASP document, such as: http://www.xrss.cn/WebRss.Asp?SortId=5

They are the embodiment of XML data, in order to realize the dynamic of XML data, so need to use a dynamic programming language, such as ASP to implement it.

Ii. How to generate a dynamic XML document

If the XML file is generated, the dynamic document is in ASP format, so you must use the FSO for the generation of XML files, such as:

The following is a reference fragment:
<%
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 Contents </a>

For an operation on the FSO see
Http://www.xrss.cn/Info/5062.Html

In the case of generating dynamic XML data files, it is possible to control the name and value of the XML node by means of the program in the dynamic document by the MyFile.WriteLine related content.

Iii. how to generate XML data using dynamic documents

So if you're not generating an XML file and you're outputting XML data directly on a dynamic document, you need to declare the type of file (i.e. Response.ContentType)

<%response.contenttype = "Text/xml"%>

For example, directly browse the following dynamic ASP document, in the browser next to display as XML data tree

The following is a reference fragment:
<%
With Response
. ContentType = "Text/xml"
. Write ("<?xml version=" "1.0" "encoding=" "gb2312" "?>")
. Write ("< World >")
. Write ("< hello >hello,world</Hello >")
. Write ("</World >")
End With
%>


The advantage of the generated XML file is that documents that process the XML data can be static documents, such as HTML files that parse XML through JavaScript, XMLDOM, and also ease data retention, while dynamic XML data on dynamic documents is not. However, in today's dynamic documents everywhere in the era, it seems that this advantage for some applications is not very influential, even, dynamic document XML data flow instead more advantages: more timely, more dynamic.

Iv. generate XML data that's it, all right?

Whether by generating a specific XML file or a dynamic XML data stream, you can simply export the relevant XML nodes and values in the format of XML, so that XML seems simple. But this does not really touch the XML operation. In our view, these XML is nothing more than a pair of tags and related characters composed of data records, there is no vitality to speak. In reality, however, manipulating XML through XMLDOM shows the absolute advantages of XML (this is not an obvious advantage when generating XML, but it experiences infinity when adding and deleting XML nodes).

Using XMLDOM to create an XML document, you can use the Save method to generate an XML document, create an XML element using the CreateElement method, CreateNode Create a node, and, in fact, choose one of these for any label in the XML. However, you typically use createelement to create a top-level (root) element, and you use CreateNode to create child nodes (elements), and of course createelement and CreateNode are different ways to use them.


The following is a reference fragment:
<%
Set objXMLDoc = CreateObject ("Microsoft.XMLDOM")
Set world=objxmldoc.createelement ("World")
Objxmldoc.appendchild (World)
Set Hello=objxmldoc.createnode ("element", "Hello", "")
Hello. Text = "Hello,world"
ObjXMLdoc.documentElement.appendChild (Hello)
Objxmldoc.save Server.MapPath ("Test2.xml")
Set objXMLDoc = Nothing
%>

CreateObject ("Microsoft.XMLDOM") declares the use of XMLDOM objects
When an element or node is built (createelement, CreateNode), it is not added to the file tree, and to add nodes to the file tree, you need to insert, such as appendchild.
Xmldocument.createnode (type, name, NamespaceURI) represents a new node that creates a specified type, name, and namespace
Type is used to confirm the node type to be established, name is a string to confirm the name of the new node, and the prefix of the namespace is optional. NamespaceURI is a string that defines the namespace URI. If the prefix is contained in the name parameter, the node is established in the NamespaceURI text with the specified prefix. If you do not include a prefix, the specified namespace is considered a preset namespace.
Objxmldoc.createnode ("element", "Hello", "") Equals objxmldoc.createelement ("Hello")
4,objxmldoc.documentelement.appendchild (hello) is actually a node under the root element of the XML document, in this case it is equivalent to World.appendchild (hello), the world is the node name in this example, Analogy
So you can write this:


The following is a reference fragment:
<%
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 is important to note that the XML files generated through XMLDOM are UTF-8 formatted, which is a good introduction to the UTF-8 of all our application files.

Summarize

Generate XML data, you can use the FSO, such as the FSO is disabled, you can use XMLDOM, of course, you can also directly use dynamic documents. However, if you master the operation of XML, XMLDOM operations are necessary.



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.