Page 1 of 2 12 LastLast
Results 1 to 40 of 53

Thread: Indenting XML properly using MSXML4

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Indenting XML properly using MSXML4

    Could you guys help me with a way to indent XML properly using MSXML4 and nodes (instead of using InStr to find < and />). I am looking for a clean and proper way to do it.

    I currenly have something like this:
    <PropertyBag><CProject name="TestProj1"><FileName>test.lfp</FileName></CProject></PropertyBag>


    and I would like it to turn out like this:
    Code:
    <PropertyBag>
        <CProject name="TestProj1">
            <FileName>test.lfp</FileName>
        </CProject>
    </PropertyBag>
    Notice how the inner-most node contents doesn't go on a new line. I think it looks better this way.

    I have searched the forums and google and not found anything. I'm not good with MSXML so I don't even know where to start.

    Also, having the option to use 4 spaces or tabs would be great.

    Thanks a ton.
    Last edited by eyeRmonkey; Oct 14th, 2005 at 02:17 PM.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Identing XML properly using MSXML4

    I added [code][/code] tags around your XML so that your indenting shows up. I assume you are creating this XML file from VB. If so then as far as I know there is no good way to format the output so that it indents.

  3. #3

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Oops, forgot the CODE tags.

    How can there be no good way to do it in VB6?

    I won't be passing a DOMDocument or whatever to do the function that does the indenting, I will be passing a string like I posted above. The class I am using (from vbAccelerator) sends me a string like I posted above and I need to indent it.

    Here is the function they use to indent the XML (the comment at the top is the reason I am asking about a way to do it using MSXML.dll.
    VB Code:
    1. Private Function NiceXML(sXML As String) As String
    2. Dim iOpenPos As Long
    3. Dim iClosePos As Long
    4. Dim iLastPos As Long
    5. Dim iLevel As Long
    6. Dim sLevelSpace As String
    7. Dim sOut As String
    8.    ' SPM: this is quick & dirty, it could fail on CDATA sections...
    9.    ' to do it properly, use MSXML to enum the nodes instead.
    10.    iLastPos = 1
    11.    iOpenPos = InStr(iLastPos, sXML, "<")
    12.    If iOpenPos > 0 Then
    13.       If iOpenPos > 1 Then
    14.          sOut = sOut & Left$(sXML, iOpenPos)
    15.       End If
    16.       Do
    17.          iClosePos = InStr(iOpenPos, sXML, ">")
    18.          If iClosePos > 0 Then
    19.             If (Mid$(sXML, iOpenPos + 1, 1) = "/") Then
    20.                iLevel = iLevel - 1
    21.                If iLevel > 0 Then sLevelSpace = Space$(iLevel * 3) Else sLevelSpace = ""
    22.                sOut = sOut & vbCrLf & sLevelSpace & Mid$(sXML, iOpenPos, iClosePos - iOpenPos + 1) & vbCrLf
    23.             Else
    24.                sOut = sOut & sLevelSpace & Mid$(sXML, iOpenPos, iClosePos - iOpenPos + 1) & vbCrLf
    25.                iLevel = iLevel + 1
    26.                If iLevel > 0 Then sLevelSpace = Space$(iLevel * 3) Else sLevelSpace = ""
    27.             End If
    28.             iLastPos = iClosePos + 1
    29.             iOpenPos = InStr(iLastPos, sXML, "<")
    30.             If iOpenPos > iLastPos Then
    31.                sOut = sOut & sLevelSpace & Mid$(sXML, iLastPos, iOpenPos - iLastPos)
    32.             End If
    33.          End If
    34.       Loop While iOpenPos > 0
    35.    End If
    36.    If iLastPos > 0 And iLastPos < Len(sXML) Then
    37.       sOut = sOut & Mid$(sXML, iLastPos)
    38.    End If
    39.    NiceXML = sOut
    40. End Function
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Indenting XML properly using MSXML4

    I've seen similar code and as they point out there is at least one problem. I know of no way of doing the indenting via MSXML, however I'm not the last word on this. I do wonder however if there was an "enum the nodes" method then why didn't they use it?

  5. #5

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    I've only read one short begginers tutorial on using MSXML but I though there was a way to get the XML code and the text within it for a node. If you could get that couldn't you use the strucutre of the node and its parent's etc. to figure out how far tabbed it needs to be?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  6. #6

  7. #7

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    What if you stored it in a seperate string though? I mean looped through each node, use whatever property it is that returns the contents of the node AND the XML directly around it, then use the position of the node and the position of its parent to determine how many tabs out it should be, then tab it over that many, then store it in a seperate string and proceed to the next node?

    What would the problem be with that?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  8. #8

  9. #9

  10. #10

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Oh. I see what you are saying.

    I wasn't refering to putting the tabs IN the XML. I meant just being able to format the XML with tabs and then save it to a file (so that if anyone opens the file it will be readable. I don't care if they are in the actual XML at all.

    So now that we are on the same page, do you think it is possible or know how to do it? Possibly using the method I described in post #7?

    PS - Reading the link you just posted would only be a problem if you tried to tab the inner-most element, but if you did it like this:

    Code:
    <parent>
        <child1>
            <child2a>value</child2a>
            <child2b>another value</child2b>
        </child1>
    </parent>
    Then I don't think any parsers would have trouble with that. I don't think it would matter if they did because the parser I am using to work with the XML is the only parser that will ever deal with it (most likely) and that parser ignores all the extra whitespace.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Indenting XML properly using MSXML4

    Why even try to read the XML? If it was supposed to be readable, it'd be saved as text.

  12. #12

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Indenting XML properly using MSXML4

    It's stored in a common format which makes it easy to transport. I don't think readablilty is a concern. I'm just saying that you would probably parse it before you try to read it, and do the formatting in there, rather than embed it in the xml code. XML could be parsed even if it was all on one line, as the tags are used to separate parts of the text.

  14. #14

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Quote Originally Posted by dglienna
    Why even try to read the XML? If it was supposed to be readable, it'd be saved as text.
    Double huh?

    XML is VERY readable and it is saved as text. Its like reading HTML, but easier. The biggest advantage to it is that is stores the data and can describe the data it stores. I open my iTumes XML library from time to time when I am looking for stuff or just want a good example of XML. I can read it easier than I can read HTML (because most HTML is written poorly).
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  15. #15

  16. #16

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Quote Originally Posted by dglienna
    It's stored in a common format which makes it easy to transport. I don't think readablilty is a concern. I'm just saying that you would probably parse it before you try to read it, and do the formatting in there, rather than embed it in the xml code. XML could be parsed even if it was all on one line, as the tags are used to separate parts of the text.
    Have you ever opened a VB .frm file in notepad? It is has a higharchial (sp?) structure. It is readable for the most part.

    I see no reason not to make it readable (indented) unless you are dealing with a HUGE file and you are worries about the spaces/tabs taking up extra space.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  17. #17

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Quote Originally Posted by MartinLiss
    dglienna is correct. All-on-one-line XML can be parsed by the DOM or other methods via code, but what we are talking about here (I think) is human readability.
    Yes we are taking about human readability. The only reason I brought up the parser not being able to read it with tabs is because I thought that is what the page you posted was talking about (but I only skimmed it).
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  18. #18

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    So does anyone enough experience with XML to help me write some stuff using MSXML to loop through the nodes and indent indent them?

    Even if someone could just show me how to loop through all the nodes recurssively, I could probably do the rest.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Indenting XML properly using MSXML4

    VB Code:
    1. Option Explicit
    2. Private oXMLDoc As MSXML2.DOMDocument40
    3. Private Sub Form_Load()
    4.     Set oXMLDoc = New DOMDocument40
    5.     oXMLDoc.Load "MyXMLFilePath"
    6.     RecurseXML oXMLDoc
    7.     Set oXMLDoc = Nothing
    8. End Sub
    9.  
    10. Public Function RecurseXML(ByRef oxmlNode As IXMLDOMNode)
    11.     Dim oxmlNodeList As IXMLDOMNodeList
    12.     Dim lngNodeIndex As Long
    13.  
    14.     Debug.Print oxmlNode.nodeName ' Or whatever you want to do
    15.     Set oxmlNodeList = oxmlNode.childNodes
    16.    
    17.     For lngNodeIndex = 0 To oxmlNodeList.length - 1
    18.         RecurseXML oxmlNodeList(lngNodeIndex)
    19.     Next
    20.  
    21. End Function

  20. #20

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Thanks Marty. I will take a look at that and see what I can do. I will post if I come up with anything.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Indenting XML properly using MSXML4

    XML allows multilines. How will your app know what should be indented.
    Think about it. It's probably not worth being readable by humans, just to look nice.

  22. #22

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    I really do think it is worth it. Maybe I am wrong though.

    I have been trying to think of a way to get it to work and I just don't think it will without the use of instr and mid and stuff. Maybe I will do it that way. Using MSXML just doesn't work when trying to do this.

    PS - 15k posts! congrats
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  23. #23
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Indenting XML properly using MSXML4

    Try this
    VB Code:
    1. Option Explicit
    2. Private oXMLDoc As MSXML2.DOMDocument40
    3. Private mintIndents As Integer
    4. Private Sub Form_Load()
    5.     Set oXMLDoc = New DOMDocument40
    6.     Me.Show
    7.     oXMLDoc.Load "MyXMLFilePath"
    8.     RecurseXML oXMLDoc, 0
    9.     Set oXMLDoc = Nothing
    10. End Sub
    11.  
    12. Public Function RecurseXML(ByRef oxmlNode As IXMLDOMNode, Indents As Integer)
    13.     Dim oxmlNodeList As IXMLDOMNodeList
    14.     Dim lngNodeIndex As Long
    15.    
    16.     Picture1.Print Space(Indents * 4) & oxmlNode.xml
    17.     Set oxmlNodeList = oxmlNode.childNodes
    18.    
    19.     If oxmlNode.parentNode Is Nothing Then
    20.         Exit Function
    21.     End If
    22.    
    23.     If oxmlNode.parentNode.hasChildNodes Then
    24.         For lngNodeIndex = 0 To oxmlNodeList.length - 1
    25.             mintIndents = mintIndents + 1
    26.             RecurseXML oxmlNodeList(lngNodeIndex), mintIndents
    27.             mintIndents = mintIndents - 1
    28.         Next
    29.     Else
    30.         Exit Function
    31.     End If
    32.    
    33. End Function

  24. #24

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Thanks for the code Marty, but that didn't work for the same reason I couldn't get mine to work. It returns something like this:
    Code:
        <PropertyBag><CProject name="testFileName"><FileName>testFileName</FileName><Title>testTitle</Title><FileCount>1</FileCount><i></i></CProject></PropertyBag>
            <CProject name="testFileName"><FileName>testFileName</FileName><Title>testTitle</Title><FileCount>1</FileCount><i></i></CProject>
                <FileName>testFileName</FileName>
                    testFileName
                <Title>testTitle</Title>
                    testTitle
                <FileCount>1</FileCount>
                    1
                <i></i>
    Theres no way to indent the first and last tags of a parent tag the right way. I think it would have to be done with InStr and Mid and stuff.

    I actually threw some XML into an HTML parser I made a while back. It had an extra feature that would autotab the HTML for you, and it works fine for XML as well, but its a TON of code and its very slow, so I would rather start over and make something from scratch than use that.

    EDIT: On second thought, maybe I could save that output to a string and use instr() to find all the extra XML at the end of each line. Then I could use the base tag name of the XML to add the </Parent> tags for the parent tags. It might be workable from that code Marty. I'll play with it later.
    Last edited by eyeRmonkey; Oct 16th, 2005 at 02:49 PM.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  25. #25

  26. #26

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Here it is:
    Code:
    <PropertyBag><CProject name="testFileName"><FileName>testFileName</FileName><Title>testTitle</Title><FileCount>1</FileCount><i></i></CProject></PropertyBag>
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  27. #27

  28. #28

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    I know. Thats the problem I was having as well. I might try what I mentioned in post #24.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  29. #29
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Indenting XML properly using MSXML4

    Quote Originally Posted by eyeRmonkey
    higharchial (sp?)
    "hierarchial"

  30. #30

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Quote Originally Posted by penagate
    "hierarchial"



    I knew that didn't look right.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  31. #31
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Indenting XML properly using MSXML4

    After extensive research I've got the solution! It involves something called an XSL transform.

    Here is the code

    VB Code:
    1. Option Explicit
    2. Private oXMLDoc As MSXML2.DOMDocument40
    3. Private Sub Form_Load()
    4.     Dim oXMLDoc As New DOMDocument40
    5.     Dim oXSLT As New DOMDocument40
    6.     Dim strResult As String
    7.      
    8.     oXMLDoc.async = False
    9.     oXSLT.async = False
    10.      
    11.     oXMLDoc.Load "My.xml"
    12.     oXSLT.Load "My.xsl"
    13.  
    14.     strResult = oXMLDoc.transformNode(oXSLT)
    15.     oXMLDoc.loadXML strResult
    16.     Picture1.Print oXMLDoc.xml
    17.  
    18. End Sub
    And here is the XSL file
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
         
         <xsl:template match="@* | node()">
              <xsl:copy>
                   <xsl:apply-templates select="@* | node()" />
              </xsl:copy>
         </xsl:template>
    </xsl:stylesheet>

  32. #32
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Indenting XML properly using MSXML4

    I asked this same question a while back on how to indent XML. At the time I need it is because I was dealing with massive XML data. It is much easier to read and edit right there if you have the file open. You can use something like XML Spy to do the edit but sometime it is much quicker to just open a file in Notepad.

    I am also like EyeRMonkey where I like it to look right for the reason that when you email the xml file to someone, they might not have XMLSpy.

    MartinLiss is on the right path of using recursive functions/subs but takes lots of coding. There are different algorithm of the open tags and close tags.

    The fastest way of doing indent for XML file is using VB.Net. I will look for the code and post it. It was just a basic output in .Net.

    I also got it to work in VB6 but had to write a several modules of code just to do make it look pretty. You will need to use XPath to find the deepest layer and work your way back out from there.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  33. #33
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Indenting XML properly using MSXML4

    Quote Originally Posted by Liquid Metal
    I asked this same question a while back on how to indent XML. At the time I need it is because I was dealing with massive XML data. It is much easier to read and edit right there if you have the file open. You can use something like XML Spy to do the edit but sometime it is much quicker to just open a file in Notepad.

    I am also like EyeRMonkey where I like it to look right for the reason that when you email the xml file to someone, they might not have XMLSpy.

    MartinLiss is on the right path of using recursive functions/subs but takes lots of coding. There are different algorithm of the open tags and close tags.

    The fastest way of doing indent for XML file is using VB.Net. I will look for the code and post it. It was just a basic output in .Net.

    I also got it to work in VB6 but had to write a several modules of code just to do make it look pretty. You will need to use XPath to find the deepest layer and work your way back out from there.
    Check out my latest post above. Recursion isn't necessary!

  34. #34
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Thumbs up Re: Indenting XML properly using MSXML4

    You are impressive Martin!
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  35. #35
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Indenting XML properly using MSXML4

    Martin, can you explain your xslt file?

    Is "xsl:copy" just a tag or is it an actual function of something.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  36. #36

  37. #37

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    WOW! Many thanks Marty! You clearly put a lot of work into figuring this one out. I really appriciate it.

    Okay, I just tested it and I get an error when I try to run the project.

    The error says: "The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document."

    I attachmed my project. Does anyone else get the same error?
    Attached Files Attached Files
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  38. #38

  39. #39

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Indenting XML properly using MSXML4

    Yeah, I was having problems with the picturebox also, thats weird.

    I fixed it myself though, I needed to specify the whole path to the XML and XSL files for some reason. No biggy though. After that it worked perfectly. Thanks a million Marty.

    I think I might package this into a module and post it into the codebank (fully crediting its original author ).
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  40. #40

Page 1 of 2 12 LastLast

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