Cenobite
Oct 23rd, 2002, 05:28 AM
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
MartinLiss
Oct 23rd, 2002, 08:14 AM
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.
Private Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" (ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long
Private Declare Function GetTempFileName Lib "kernel32" _
Alias "GetTempFileNameA" (ByVal lpszPath As String, _
ByVal lpPrefixString As String, ByVal wUnique As Long, _
ByVal lpTempFileName As String) As Long
Public Sub PrettyPrint()
'***************************************************************************
'Purpose: When nodes are added, they wind up on the same line. This does not
' affect how they are read by xml readers, but it does make them
' difficult to read in Word, etc. While this routine does not correct
' indenting, it does at least break up the run-on lines.
'Inputs: The newly-modified xml file
'Outputs: The newly-modified xml file reformatted
'***************************************************************************
Dim intInFile As Integer
Dim intOutFile As Integer
Dim strLine As String
Dim strOutputLine As String
Dim strTempFile As String
Dim bDone As Boolean
Dim intPos As Integer
On Error GoTo ErrorRoutine
strTempFile = CreateTempFile("xml")
intInFile = FreeFile
Open My_XML_File For Input As intInFile
intOutFile = FreeFile
Open strTempFile For Output As intOutFile
While Not EOF(intInFile)
Line Input #intInFile, strLine
bDone = False
Do Until bDone
intPos = InStr(1, strLine, "><")
If intPos = 0 Then
bDone = True
Print #intOutFile, strLine
Else
strOutputLine = Left$(strLine, intPos)
Print #intOutFile, strOutputLine
strLine = Right$(strLine, Len(strLine) - intPos)
End If
Loop
Wend
Close intInFile
Close intOutFile
FileCopy strTempFile, My_XML_File
Kill strTempFile
Exit Sub
ErrorRoutine:
' Your error-routine here
End Sub
Public Function CreateTempFile(strPrefix As String) As String
'***************************************************************************
'Purpose: Create a unique temporary file name
'Inputs: strPrefix - The prefix to be used as the first part of the temp
' file name. Note: only the first 3 characters get used.
'Outputs: The temp file name
'***************************************************************************
Dim strTmpPath As String * 512
Dim strTmpName As String * 576
Dim intRet As Long
intRet = GetTempPath(512, strTmpPath)
If (intRet > 0 And intRet < 512) Then
intRet = GetTempFileName(strTmpPath, strPrefix, 0, strTmpName)
If intRet <> 0 Then
CreateTempFile = Left$(strTmpName, _
InStr(strTmpName, vbNullChar) - 1)
End If
End If
End Function
Cenobite
Oct 23rd, 2002, 10:35 AM
Thanks for that. It seems rediculous that the object model doesn't allow for this though.....does anyone know of a simpler way???
MartinLiss
Oct 26th, 2002, 11:06 AM
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)