在VBScript中格式化XML字符串

4
我有一段未格式化的XML字符串(没有空格),我想编写一个VBScript函数,将该字符串作为参数并使用制表符和换行符对XML进行格式化。
我在网上搜索了很多,最接近的是这个http://blogs.msdn.com/b/robert_mcmurray/archive/2012/07/06/creating-quot-pretty-quot-xml-using-xsl-and-vbscript.aspx
但这并不起作用,因为据我所知,“MSXML2.DomDocument”对象不支持向字符串写入。
我尝试访问对象的各种属性(即“xml”,“text”和“xml.text”),但都无济于事。
简而言之,我需要输入一串混乱的XML字符串,输出一串格式化的XML字符串。
2个回答

4
所有的功劳归功于Robert McMurray;我只是将他的脚本改写成了一个函数:
Option Explicit

' ****************************************
Function prettyXml(ByVal sDirty)
' ****************************************
' Put whitespace between tags. (Required for XSL transformation.)
' ****************************************
  sDirty = Replace(sDirty, "><", ">" & vbCrLf & "<")
' ****************************************
' Create an XSL stylesheet for transformation.
' ****************************************
  Dim objXSL : Set objXSL = WScript.CreateObject("Msxml2.DOMDocument")
  objXSL.loadXML  "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" & _
                  "<xsl:output method=""xml"" indent=""yes""/>" & _
                  "<xsl:template match=""/"">" & _
                  "<xsl:copy-of select="".""/>" & _
                  "</xsl:template>" & _
                  "</xsl:stylesheet>"
' ****************************************
' Transform the XML.
' ****************************************
  Dim objXML : Set objXML = WScript.CreateObject("Msxml2.DOMDocument")
  objXML.loadXml sDirty
  objXML.transformNode objXSL
  prettyXml = objXML.xml
End Function

Dim sTest : sTest = "<a><b><c/></b></a>"
WScript.Echo           sTest
WScript.Echo "----------"
WScript.Echo prettyXml(sTest)
WScript.Quit 0

输出:

cscript robmcm-2.vbs
<a><b><c/></b></a>
----------
<a>
        <b>
                <c/>
        </b>
</a>

转念一想:

除非你已经学习过这个,否则不应使用上述内容。


非常感谢。我意识到没有必要格式化(我的脚本生成的XML是有效的)。这只是为了输出到文件/数据库。 - kr094

1

所有的功劳归功于Ekkehard。如果您想在Asp Classic应用程序中使用该脚本,我已经进行了修改。

Function prettyXml(ByVal sDirty)
    '****************************************
    '* Put whitespace between tags. (Required for XSL transformation.)
    '****************************************
    sDirty = Replace(sDirty, "><", ">" & vbLf & "<")

    '****************************************
    '* Create an XSL stylesheet for transformation.
    '****************************************
    Dim objXSL : Set objXSL = Server.CreateObject("Msxml2.DOMDocument")
    objXSL.loadXML  "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" & _
                    "<xsl:output method=""xml"" indent=""yes""/>" & _
                    "<xsl:template match=""/"">" & _
                    "<xsl:copy-of select="".""/>" & _
                    "</xsl:template>" & _
                    "</xsl:stylesheet>"

    '****************************************
    '* Transform the XML.
    '****************************************
    Dim objXML : Set objXML = Server.CreateObject("Msxml2.DOMDocument")
    objXML.loadXml sDirty
    objXML.transformNode objXSL
    prettyXml = objXML.xml
End Function

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接