在 VBA 中输出 MSXML2 XML 文档的文本时如何获得新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5305947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:50:07  来源:igfitidea点击:

How do I get new lines when outputting the text for MSXML2 XML document in VBA?

xmlvbamsxml

提问by HorusKol

I'm generating an XML document in VBA using the MSXML2.DOMDocument and then sending the XML property of the object to a remote server (via POST).

我正在使用 MSXML2.DOMDocument 在 VBA 中生成一个 XML 文档,然后将对象的 XML 属性发送到远程服务器(通过 POST)。

The resulting string in MSXML2.DOMDocument.XML has no newlines, and so it is one big blob of XML. Is there a way to get the ouptput to put a new line after every XML element, making the file more human readable?

MSXML2.DOMDocument.XML 中的结果字符串没有换行符,因此它是一大块 XML。有没有办法让输出在每个 XML 元素之后添加一个新行,使文件更易读?

This isn't exactly essential, as the file received on the server is going to be immediately parsed and the information stored in an SQL database, but this would help development and testing.

这并不是必需的,因为服务器上接收到的文件将被立即解析并将信息存储在 SQL 数据库中,但这将有助于开发和测试。

采纳答案by rsbarro

You could just replace every instance of ">"with ">" & vbCrLfusing the Replacefunction before writing the file to disk.

你可以只更换的每个实例">"">" & vbCrLf使用Replace该文件写入磁盘之前的功能。

Or you could just save the XML to disk as is and open it with Firefox or IE which should do the syntax highlighting for you.

或者,您可以将 XML 按原样保存到磁盘,然后使用 Firefox 或 IE 打开它,它们应该为您执行语法突出显示。

回答by Eric G

Don't know if you've found a suitable solution/workaround, but I was looking at this issue in msxml and someone suggested using MXXMLWriter. You can pipe the output through a SAX parser (SAXXMLReader) and hook MXXMLWriter to it, as described here. It's somewhat ridiculous to have to re-parse the xml to get it formatted when you know the DOMDocument has all the information it needs to indent, but there you have it.

不知道您是否找到了合适的解决方案/解决方法,但我正在 msxml 中查看此问题,有人建议使用 MXXMLWriter。可以通过管道通过SAX解析器(SAXXMLReader中)和钩MXXMLWriter给它,如所描述的输出在这里。当您知道 DOMDocument 具有它需要缩进的所有信息时,不得不重新解析 xml 以对其进行格式化有点荒谬,但您已经拥有了。

Actually I didn't have time to explore yet but apparently you can use MXXMLWriter unattached to a SAX reader to build the XML - instead of using DOMDocment - and get it to indent as you go instead. See instructions here.

实际上,我还没有时间进行探索,但显然您可以使用未附加到 SAX 阅读器的 MXXMLWriter 来构建 XML - 而不是使用 DOMDocment - 并在您进行时让它缩进。请参阅此处的说明。

回答by Darth Jurassic

Just wrote a VBA sub that indents XML document before saving it. Indentation is similar to what I see when formatting it in Visual Studio. Maybe it should be extended to handle not only subelements and CDatas.

刚刚编写了一个 VBA 子程序,在保存之前缩进 XML 文档。缩进与我在 Visual Studio 中对其进行格式化时看到的类似。也许它应该扩展到不仅处理子元素和 CDatas。

Sub IndentXml(xml As IXMLDOMElement, Optional depth As Integer)
    If IsMissing(depth) Then
        depth = 0
    End If

    Dim txt As IXMLDOMText

    If Not (xml.OwnerDocument.DocumentElement Is xml) Then
        Set txt = xml.OwnerDocument.createTextNode(vbNewLine & String(depth, vbTab))
        xml.ParentNode.InsertBefore txt, xml
    End If

    Dim child As IXMLDOMNode
    Dim hasElements As Boolean
    hasElements = False
    For Each child In xml.ChildNodes
        If child.NodeType = NODE_ELEMENT Then
            IndentXml child, depth + 1
            hasElements = True
        ElseIf child.NodeType = NODE_CDATA_SECTION Then
            Set txt = xml.OwnerDocument.createTextNode(vbNewLine & String(depth + 1, vbTab))
            xml.InsertBefore txt, child
            hasElements = True
        End If
    Next

    If hasElements Then
        Set txt = xml.OwnerDocument.createTextNode(vbNewLine & String(depth, vbTab))
        xml.appendChild txt
    End If
End Sub

回答by Mike R

Thanks Darth Jurassic for your code. Very handy!

感谢 Darth Jurassic 的代码。非常便利!

In my own code the second "If" block tripped up on "xml.ParentNode" being Nothing. For my own purposes I changed the following line from:

在我自己的代码中,第二个“If”块在“xml.ParentNode”什么都没有时绊倒了。出于我自己的目的,我更改了以下行:

If Not (xml.OwnerDocument.DocumentElement Is xml) Then

to:

到:

If depth > 0 Then

This worked fine for what I need so thought I'd share it.

这对我需要的东西很有效,所以我想我会分享它。

回答by ja72

Maybe you should look at the freeware ChilKat Xmlinstead of the horrid MSXMLand make your life easy. I remember struggling with indent and formatting with MSXML, and I know how frustrating it is.

也许你应该看看免费软件ChilKat Xml而不是可怕的MSXML,让你的生活更轻松。我记得在使用 MSXML 进行缩进和格式设置时苦苦挣扎,我知道这有多令人沮丧。

Good luck.

祝你好运。