PDA

Click to See Complete Forum and Search --> : XML Line Feeds


cls224
Jan 31st, 2001, 09:32 AM
I need some help. I made a vb program with the XML DOM object that creates and XML file. Everything is fine and another program can read that xml file fine. The problem is that the other morons sitting around me would like the xml formatted. If you open the xml file in IE it looks fine... But if you open it in say notepad its all messed up on 2 lines. Is there anyway with the XML DOM object to insert a line feed so you can view the file in notepad? I know its pointless but hey... im still on the clock.
Thanks

Jerry Grant
Feb 6th, 2001, 05:58 AM
This does it in the simplest form!

Private Function FormatXML(sXML As String) As String
Dim iOpenPos As Long
Dim iClosePos As Long
Dim iLastPos As Long
Dim iLevel As Long
Dim sLevelSpace As String
Dim sOut As String
' JJG: this is quick & dirty, it could fail on CDATA sections...
' to do it properly, use MSXML to enum the nodes instead.
iLastPos = 1
iOpenPos = InStr(iLastPos, sXML, "<")
If iOpenPos > 0 Then
If iOpenPos > 1 Then
sOut = sOut & Left$(sXML, iOpenPos)
End If
Do
iClosePos = InStr(iOpenPos, sXML, ">")
If iClosePos > 0 Then
If (Mid$(sXML, iOpenPos + 1, 1) = "/") Then
iLevel = iLevel - 1
If iLevel > 0 Then sLevelSpace = Space$(iLevel * 3) Else sLevelSpace = ""
sOut = sOut & vbCrLf & sLevelSpace & Mid$(sXML, iOpenPos, iClosePos - iOpenPos + 1) & vbCrLf
Else
sOut = sOut & sLevelSpace & Mid$(sXML, iOpenPos, iClosePos - iOpenPos + 1) & vbCrLf
iLevel = iLevel + 1
If iLevel > 0 Then sLevelSpace = Space$(iLevel * 3) Else sLevelSpace = ""
End If
iLastPos = iClosePos + 1
iOpenPos = InStr(iLastPos, sXML, "<")
If iOpenPos > iLastPos Then
sOut = sOut & sLevelSpace & Mid$(sXML, iLastPos, iOpenPos - iLastPos)
End If
End If
Loop While iOpenPos > 0
End If
If iLastPos > 0 And iLastPos < Len(sXML) Then
sOut = sOut & Mid$(sXML, iLastPos)
End If
FormatXML = sOut
End Function