Results 1 to 4 of 4

Thread: Saving XML???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Bristol, UK
    Posts
    86

    Saving XML???

    Hi guys,
    I'm new to this XML stuff and something is really bugging me...
    If I save my XML using the MS implementation of the DOM and then open the resulting file in notepad, the XML is all written on one line .... ie unformatted! Is there anyway to format it to make it more readable? I notice that if you read in a formatted file and then write it back out it gets written in a formatted form.

    Any ideas???

    Cheers

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I have never found a totally satisfactory solution, and I have asked the same question on this and other forums. In one respect it doesn't matter because if you open the file using a browser or XML Spy, it will appear properly formatted, but I know it's nice to be able to look at it in Word, Notepad, etc.

    I wrote the following routine that at least breaks up the lines. What I am posting here is a version with some non-applicable code removed and I haven't tested it, so if it doesn't work please let me know. Also I would be very interested in any better solution.
    VB Code:
    1. Private Declare Function GetTempPath Lib "kernel32" _
    2.          Alias "GetTempPathA" (ByVal nBufferLength As Long, _
    3.          ByVal lpBuffer As String) As Long
    4.  
    5. Private Declare Function GetTempFileName Lib "kernel32" _
    6.          Alias "GetTempFileNameA" (ByVal lpszPath As String, _
    7.          ByVal lpPrefixString As String, ByVal wUnique As Long, _
    8.          ByVal lpTempFileName As String) As Long
    9.  
    10. Public Sub PrettyPrint()
    11. '***************************************************************************
    12. 'Purpose: When nodes are added, they wind up on the same line. This does not
    13. '         affect how they are read by xml readers, but it does make them
    14. '         difficult to read in Word, etc. While this routine does not correct
    15. '         indenting, it does at least break up the run-on lines.
    16. 'Inputs:  The newly-modified xml file
    17. 'Outputs: The newly-modified xml file reformatted
    18. '***************************************************************************
    19.  
    20.     Dim intInFile As Integer
    21.     Dim intOutFile As Integer
    22.     Dim strLine As String
    23.     Dim strOutputLine As String
    24.     Dim strTempFile As String
    25.     Dim bDone As Boolean
    26.     Dim intPos As Integer
    27.    
    28.     On Error GoTo ErrorRoutine
    29.    
    30.     strTempFile = CreateTempFile("xml")
    31.    
    32.     intInFile = FreeFile
    33.     Open My_XML_File For Input As intInFile
    34.    
    35.     intOutFile = FreeFile
    36.    
    37.     Open strTempFile For Output As intOutFile
    38.    
    39.     While Not EOF(intInFile)
    40.         Line Input #intInFile, strLine
    41.         bDone = False
    42.         Do Until bDone
    43.             intPos = InStr(1, strLine, "><")
    44.             If intPos = 0 Then
    45.                 bDone = True
    46.                 Print #intOutFile, strLine
    47.             Else
    48.                 strOutputLine = Left$(strLine, intPos)
    49.                 Print #intOutFile, strOutputLine
    50.                 strLine = Right$(strLine, Len(strLine) - intPos)
    51.             End If
    52.         Loop
    53.     Wend
    54.  
    55.     Close intInFile
    56.     Close intOutFile
    57.  
    58.     FileCopy strTempFile, My_XML_File
    59.    
    60.     Kill strTempFile
    61.    
    62.     Exit Sub
    63.  
    64. ErrorRoutine:
    65.  
    66.     ' Your error-routine here
    67.  
    68. End Sub
    69.  
    70. Public Function CreateTempFile(strPrefix As String) As String
    71. '***************************************************************************
    72. 'Purpose: Create a unique temporary file name
    73. 'Inputs:  strPrefix - The prefix to be used as the first part of the temp
    74. '         file name. Note: only the first 3 characters get used.
    75. 'Outputs: The temp file name
    76. '***************************************************************************
    77.  
    78.     Dim strTmpPath As String * 512
    79.     Dim strTmpName As String * 576
    80.     Dim intRet As Long
    81.    
    82.     intRet = GetTempPath(512, strTmpPath)
    83.     If (intRet > 0 And intRet < 512) Then
    84.        intRet = GetTempFileName(strTmpPath, strPrefix, 0, strTmpName)
    85.        If intRet <> 0 Then
    86.           CreateTempFile = Left$(strTmpName, _
    87.              InStr(strTmpName, vbNullChar) - 1)
    88.        End If
    89.     End If
    90.  
    91. End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Bristol, UK
    Posts
    86
    Thanks for that. It seems rediculous that the object model doesn't allow for this though.....does anyone know of a simpler way???

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I just had a thought. The first time you split a line using my code, you could do this

    strOutputLine = Space(4) & Left$(strLine, intPos)

    then if you could keep track somehow of how many indents you had previously made you could do something like

    strOutputLine = Space(8) & Left$(strLine, intPos)

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