在VBScript中格式化XML字符串

时间:2014-07-31 19:56:44

标签: xml vbscript

嘿,伙计们在漫长的一天结束时快速提问。

我有一串未格式化的xml(没有whitespacing),我想创建一个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 个答案:

答案 0 :(得分:2)

归功于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>

第二个想法:

除非您已经研究过this,否则您不应该使用上述内容。

答案 1 :(得分:0)

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
相关问题