Programming: how to generate XML data

Source: Internet
Author: User
First, we must understand what the ultimate requirement is. what we need through asp or other dynamic programming languages is the XML Schema data, 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

First, you must understand what the ultimate goal is.

Through asp or other dynamic programming languages, what we ultimately need is the XML pattern of data, 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 considered asp document, such as: http://www.cnbruce.com/blog/rss2.asp

They are the embodiment of XML data. in order to realize the dynamics of XML data, we need to apply it to dynamic programming languages, such as ASP to realize it.

2. how to use daily active XML documents

For example, if it is a natural XML file, a dynamic file is in ASP format. Therefore, you must use FSO to create an XML file. for example:

<%

Xmlfile = server. mappath ("test1.xml ")

Set fso = CreateObject ("Scripting. FileSystemObject ")

Set MyFile = fso. CreateTextFile (xmlfile, True)

MyFile. WriteLine (" ")

MyFile. WriteLine (" <世界> ")

MyFile. WriteLine (" <你好> Hello, world ")

MyFile. WriteLine ("")

MyFile. Close

%>

View XML file content

For more information about FSO control, see

Http://www.cnbruce.com/blog/showlog.aspcat_id=26&log_id=440

For example, in a Daily active XML data file, the name and value of the XML node can be controlled by procedural means in the dynamic document of the related content of MyFile. WriteLine.

3. how to apply natural XML data of dynamic documents

If the XML file is not a natural XML file and the 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.

<%

With Response

. ContentType = "text/XML"

. Write (" ")

. Write (" <世界> ")

. Write (" <你好> Hello, world ")

. Write ("")

End

%>

The advantage of a natural XML file is that the document that processes the XML data can be a static document.

For example, HTML files use Javascript and XMLDOM to parse XML files and are easy to retain data, while dynamic XML data on dynamic documents is not. However, in today's era of ubiquitous use of dynamic documents, it seems that this advantage has no impact on some exploitation, or even the XML data stream of dynamic documents has a higher advantage: more timely and dynamic.

4. is this the case for native XML data?

Whether it is a natural XML file or a dynamic XML data stream, as long as the XML node and value are output according to the XML pattern, XML seems very simple. However, this does not really involve XML control. In our opinion, these XML is nothing more than a data record consisting of paired tags and coherent characters. However, in fact, holding XML through XMLDOM shows that XML has the best advantage (this is not obvious when XML is born, but it has unlimited experience when adding or deleting XML nodes ).

Use XMLDOM to create XML documents. you can use the Save method to create XML documents. use the createElement method to create XML elements and createNode to create nodes, you can select either of the tags in XML. However, createElement is used to create top-level (root) elements and createNode is used to create subnodes (elements ), of course, the application methods of createElement and createNode are also different.


<%

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

%>

CreateObject ("Microsoft. XMLDOM") declares the application XMLDOM object

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.
XmlDocument. createNode (type, name, nameSpaceURI) creates a new node with a 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. 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:

<%

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 created by XMLDOM are all UTF-8 patterns, which makes a good introduction to all our UTF-8 using program files.

Summary

Native XML data can be used to apply FSO, such as FSO, XMLDOM, and dynamic documents. However, if the XML control is fully grasped, XMLDOM control is necessary.



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.