CodeItBetter Programming Another VB Programming Blog

Indent XML Code

Posted on June 27, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'XML Handling - Indent XML Code
Sub oIndentXml(oXMLfile As String)
    On Error Resume Next
    ''This procedure is used to indent XML document
    '' <Family>
    ''    <Member Relationship="Father">
    ''       <Name>Some Guy</Name>
    ''    </Member>
    '' </Family>
    '' Requires msxml.dll (Go to Project --> References and and choose Microsoft
    '' XML version 2.0, or whatever the
    '' current version you have installed)
    Dim oXSLT As DOMDocument
    Dim XSL_FILE As String
    Dim sResult As String
    Const DoubleQuotes = """"
    Dim strText As String
    Dim objDom As DOMDocument
 
    Set objDom = New DOMDocument
 
    Open oXMLfile For Input As #1
    strText = Input$(LOF(1), #1)
    Close #1
 
    objDom.loadXML strText
 
    Set oXSLT = New DOMDocument
    XSL_FILE = "<?xml version=" & DoubleQuotes & "1.0" & DoubleQuotes & " encoding=" & DoubleQuotes _
        & "UTF-8" & DoubleQuotes & "?>" & vbCrLf & "<xsl:stylesheet version=" & DoubleQuotes & "1.0" _
        & DoubleQuotes & " xmlns:xsl=" & DoubleQuotes & "http://www.w3.org/1999/XSL/Transform" _
        & DoubleQuotes & ">" & vbCrLf & "     <xsl:output method=" & DoubleQuotes & "xml" & DoubleQuotes _
        & " version=" & DoubleQuotes & "1.0" & DoubleQuotes & " encoding=" & DoubleQuotes & "UTF-8" _
        & DoubleQuotes & " indent=" & DoubleQuotes & "yes" & DoubleQuotes & "/>" & vbCrLf & _
        "     <xsl:template match=" & DoubleQuotes & "@* | node()" & DoubleQuotes & ">" & vbCrLf & _
        "          <xsl:copy>" & vbCrLf        & "               <xsl:apply-templates select=" & DoubleQuotes & _
        "@* | node()" & DoubleQuotes & " />" & vbCrLf & "          </xsl:copy>" & vbCrLf & "     </xsl:template>" _
        & vbCrLf & "</xsl:stylesheet>"
    objDom.async = False
    oXSLT.async = False
    oXSLT.loadXML XSL_FILE
    If oXSLT.parseError.errorCode = 0 Then
        If oXSLT.readyState = 4 Then
            sResult = objDom.transformNode(oXSLT.documentElement)
            sResult = Replace$(sResult, "<?xml version=" & DoubleQuotes & "1.0" & DoubleQuotes & " encoding=" _
                & DoubleQuotes & "UTF-16" & DoubleQuotes & "?>", vbNullString, , , vbTextCompare)
            objDom.loadXML sResult
        End If
    Else
        Debug.Print Err.Description = oXSLT.parseError.reason & vbCrLf & "Line: " & oXSLT.parseError.Line & vbCrLf _
            & "XML: " & oXSLT.parseError.srcText
        'Err.Raise 1006
        Err.Clear
    End If
 
    strText = objDom.xml
 
    'open the file to save added headers after indenting the code
    Open oXMLfile For Output As #1
    Print #1, strText
    Close #1
 
ExitHere:
    Err.Clear
    Set oXSLT = Nothing
    Set objDom = Nothing
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.