First, you must find out what is ultimately needed
We use ASP or other dynamicProgramming Language, The final need is XML format 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 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:
<%
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
%>
<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.
<%
With response
. Contenttype = "text/XML"
. Write ("<? XML version = "" 1.0 "" encoding = "" gb2312 ""?> ")
. Write ("<world> ")
. Write ("<Hello> Hello, world </Hello> ")
. Write ("</World> ")
End
%>
4. Is this the way 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.
<%
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 use of xmldom objects
- 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) 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. 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 generated through xmldom are in UTF-8 format, which applies to all of our applicationsProgramThe UTF-8 of the document has become a good recommendation.
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.
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.