Results 1 to 2 of 2

Thread: XML Line Feeds

  1. #1

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    38

    Exclamation

    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

  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    This does it in the simplest form!
    Code:
    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
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width