如何使用 VB 6.0 生成格式良好的 XML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10202576/
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
How to Generate a well formatted XML file using VB 6.0?
提问by
I'm working on Visual Basic 6.0 Project and i need to generate a well formatted XML file whose looks like this:
我正在处理 Visual Basic 6.0 项目,我需要生成一个格式良好的 XML 文件,其如下所示:
<Myinfo>
<FirstName>My First Name</FirstName>
<LastName>My Last Name</LastName>
<StreetAdd>My Address</StreetAdd>
<MyInfo>
Note: i got the job done generating the XML file, but i'm still in need for the right formatting as shown above.
注意:我完成了生成 XML 文件的工作,但我仍然需要正确的格式,如上所示。
The XML file i generated is formatted like in one single line like this:
我生成的 XML 文件的格式类似于这样的一行:
<Myinfo><FirstName>My First Name</FirstName><LastName>My Last Name</LastName><StreetAdd>My Address</StreetAdd><MyInfo> .
采纳答案by Tomalak
I've made a small XML pretty printer that works quite well:
我制作了一个小型的 XML 漂亮打印机,效果很好:
Sub PrettyPrint(Parent As IXMLDOMNode, Optional Level As Integer)
Dim Node As IXMLDOMNode
Dim Indent As IXMLDOMText
If Not Parent.ParentNode Is Nothing And Parent.ChildNodes.Length > 0 Then
For Each Node In Parent.ChildNodes
Set Indent = Node.OwnerDocument.createTextNode(vbNewLine & String(Level, vbTab))
If Node.NodeType = NODE_TEXT Then
If Trim(Node.Text) = "" Then
Parent.RemoveChild Node
End If
ElseIf Node.PreviousSibling Is Nothing Then
Parent.InsertBefore Indent, Node
ElseIf Node.PreviousSibling.NodeType <> NODE_TEXT Then
Parent.InsertBefore Indent, Node
End If
Next Node
End If
If Parent.ChildNodes.Length > 0 Then
For Each Node In Parent.ChildNodes
If Node.NodeType <> NODE_TEXT Then PrettyPrint Node, Level + 1
Next Node
End If
End Sub
You call it by passing in the DOMDocumentobject and leaving the Levelparameter blank.
您可以通过传入DOMDocument对象并将Level参数留空来调用它。
- It does an in-place modification of the document.
- You will lose all insignificant whitespace (blanks between XML elements) that might have been there.
- It uses one tab to indent.
- It also indents comments and processing instructions etc.
- it will work with all versions of
DOMDocument.
- 它对文档进行就地修改。
- 您将丢失所有可能存在的无关紧要的空格(XML 元素之间的空格)。
- 它使用一个制表符来缩进。
- 它还缩进注释和处理指令等。
- 它将适用于所有版本的
DOMDocument.
Dim XmlDoc as New MSXML2.DOMDocument40
' create/load your xml document
PrettyPrint XmlDoc
MsgBox XmlDoc.xml
There also is an easy way to do it via SAX.
还有一种简单的方法可以通过 SAX做到这一点。
回答by Deanna
You really should be using an XML library to create "correct" XML. It handles encoding, data formats, etc
您确实应该使用 XML 库来创建“正确”的 XML。它处理编码、数据格式等
Set XMLDoc = New DOMDocument
Set XMLRoot = XMLDoc.appendChild(XMLDoc.createElement("Myinfo"))
XMLRoot.appendChild(XMLDoc.createElement("FirstName")).Text = "My First Name"
XMLRoot.appendChild(XMLDoc.createElement("LastName")).Text = "My Last Name"
XMLRoot.appendChild(XMLDoc.createElement("StreetAdd")).Text = "My Address"
XMLDoc.xmlwill then output valid XML, or you can pretty print it as Tomalak suggests if you really want to.
XMLDoc.xml然后将输出有效的 XML,或者如果您真的想要,您可以按照 Tomalak 的建议将其打印出来。
回答by Zhenya
There's a little improvement to Tomalak's code, I'm adding a vbNewLine after the end of the tag:
Tomalak 的代码有一点改进,我在标签末尾添加了一个 vbNewLine:
Private Sub FormatOuterXml(ByRef Parent As IXMLDOMNode, Optional ByVal Lvl As Long = 0)
If Parent.ParentNode Is Nothing Or Parent.ChildNodes.Length = 0 Then Exit Sub
Dim xn0 As IXMLDOMNode, id0 As IXMLDOMText, id1 As IXMLDOMText
Set id0 = Parent.OwnerDocument.createTextNode(vbNewLine & String(Lvl, vbTab))
If Lvl > 0 Then Set id1 = Parent.OwnerDocument.createTextNode(vbNewLine & String(Lvl - 1, vbTab))
For Each xn0 In Parent.ChildNodes
If xn0.NodeType = MSXML2.NODE_TEXT Then
If LenB(xn0.Text) = 0 Then Parent.RemoveChild xn0
ElseIf xn0.PreviousSibling Is Nothing Then
Parent.InsertBefore id0.CloneNode(True), xn0
ElseIf xn0.PreviousSibling.NodeType <> MSXML2.NODE_TEXT Then
Parent.InsertBefore id0.CloneNode(True), xn0
ElseIf xn0.NextSibling Is Nothing Then
If Not id1 Is Nothing Then Parent.appendChild id1.CloneNode(True)
End If
Next xn0
If Parent.ChildNodes.Length > 0 Then
For Each xn0 In Parent.ChildNodes
If xn0.NodeType <> MSXML2.NODE_TEXT Then FormatOuterXml xn0, Lvl + 1
Next xn0
End If
End Sub
回答by Ira Baxter
You need to count the nesting of the tags, and output indentation corresponding to the depth of nesting. Something like (forgive my bad VB6 syntax):
需要统计标签的嵌套次数,输出对应嵌套深度的缩进。像(原谅我糟糕的 VB6 语法):
Int Nesting=0
Bool LastOutputWasText=false
Sub XMLIndent
Do I=1,Nesting
Print " ";
End Do
End Sub
Sub XMLOutputOpenTag(String OpenTag)
if LastOutputWasText then
Print
endif
XMLIndent
Nesting=Nesting+1
Print OpenTag;
LastOutputWasText=true
End Sub
Sub XMLOutputCloseTag(String CloseTag)
! Always call OpenTag and CloseTag with matching Tag names
if !LastOutputWasText then
XMLIndent
endif
Print CloseTag
Nesting=Nesting-1
LastOutputWasText=false
End Sub
Sub XMLOutputText(String Text)
Print Text;
LastOutputWasText=true
End Sub
OP left to revise this to write to a file or whereever he wants the result.
OP 留下来修改它以写入文件或他想要结果的任何地方。

