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>
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.
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:
Private Function NiceXML(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
' SPM: 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 = ""
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?
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?
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?
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:
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.
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.
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).
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.
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.
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).
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.
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.
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.
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 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!
I ran your project and it ran without errors but the output didn't show up in the picturebox. I then swapped my picturebox for yours and it ran perfectly.
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 ).