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"
%>