Create an XML file in ASP and enable auto indent

Source: Internet
Author: User

When using DOM in ASP to create a new document, the new document will not be automatically indented. Although the new document is in a good format or valid format in terms of the document content, the entire document is on one line, when you open a new file using notepad or other editing work, it is inconvenient to browse the file. The following two solutions are provided to indent XML in ASP:
1. Use XSL for indent formatting:
XSL file:

<? XML version = "1.0"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: output method = "XML" version = "1.0" encoding = "UTF-8" omit-XML-declaration = "yes" indent = "yes" media-type = "text/XML "/>

<XSL: template match = "/| @ * | node ()">
<XSL: Copy>
<XSL: Apply-templates select = "@ * | node ()"/>
</XSL: Copy>
</XSL: Template>
</XSL: stylesheet>

Bytes ----------------------------------------------------------------------------------------------------------------------
Bytes ----------------------------------------------------------------------------------------------------------------------
ASP:
<%
Dim xmldoc, pull doc, resultdoc
Dim node, subnode
Set xmldoc = server. Createobject ("msxml2.domdocument ")
Xmldoc. async = false
Xmldoc. loadxml ("<? XML version = '1. 0' encoding = 'utf-8'?> <Root/> ") 'load the root node

'-------------------- Create a node * Start ----------------------
Set node = xmldoc. createelement ("user ")
Set subnode = xmldoc. createelement ("name ")
Subnode. Text = "ssm1226"
Node. appendchild subnode
Set subnode = xmldoc. createelement ("nickname ")
Subnode. Text = "Rain Man"
Node. appendchild subnode
Set subnode = nothing
Xmldoc.doc umentelement. appendchild Node
Set node = nothing
'---------------------- Create a node * end ----------------------
Set destination Doc = server. Createobject ("msxml2.domdocument ")
Optional Doc. async = false
Export Doc. Load server. mappath ("transform. XSL") 'loads and converts the XSL File

Set resultdoc = server. Createobject ("msxml2.domdocument ")
Resultdoc. async = false
Xmldoc. transformnodetoobject conversion doc, resultdoc 'Conversion
Set node = resultdoc. createprocessinginstruction ("XML", "version = '1. 0' encoding = 'utf-8'") 'chuangjia <? XML version = '1. 0' encoding = 'utf-8'?> Statement
Resultdoc. insertbefore node, resultdoc. firstchild add Declaration
Set node = nothing
Resultdoc. Save server. mappath ("result. xml") 'Save the XML file
%>

2. Add a line break to the DOM

<%
Dim xmldoc, node1, node2
Set xmldoc = server. Createobject ("msxml2.domdocument ")
Xmldoc. async = false
Xmldoc. validateonparse = false
If not xmldoc. loadxml ("<root> </root>") then
Response. Write xmldoc. parseerror. Reason
Response. End
End if

Response. Write xmldoc. xml
Response. End
Set node1 = xmldoc. createtextnode (vbcrlf)

Set node2 = xmldoc. createelement ("name ")
Node2.text = "ssm1226"

Xmldoc.doc umentelement. appendchild node1.clonenode (true)
Xmldoc.doc umentelement. appendchild node2
Xmldoc.doc umentelement. appendchild node1.clonenode (true)

Xmldoc. Save "C:/test. xml"

%>

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.