我正在使用Visual Basic 6.0 Project,我需要生成格式良好的XML文件,如下所示:
<Myinfo>
<FirstName>My First Name</FirstName>
<LastName>My Last Name</LastName>
<StreetAdd>My Address</StreetAdd>
<MyInfo>
注意:我完成了生成XML文件的工作,但我仍然需要正确的格式,如上所示。
我生成的XML文件的格式如下一行:
<Myinfo><FirstName>My First Name</FirstName><LastName>My Last Name</LastName><StreetAdd>My Address</StreetAdd><MyInfo> .
答案 0 :(得分:2)
我制作了一款效果很好的小型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
您可以通过传递DOMDocument
对象并将Level
参数留空来调用它。
DOMDocument
的所有版本。Dim XmlDoc as New MSXML2.DOMDocument40
' create/load your xml document
PrettyPrint XmlDoc
MsgBox XmlDoc.xml
还有一种简单的方法via SAX。
答案 1 :(得分:1)
您确实应该使用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.xml
将输出有效的XML,或者你可以像Tomalak建议的那样将其打印出来,如果你真的想要的话。
答案 2 :(得分:0)
您需要计算标记的嵌套,并输出与嵌套深度相对应的缩进。像(原谅我糟糕的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左边修改它以写入文件或者他想要结果。
答案 3 :(得分:0)
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