Discussion Locked

Formatting of generated XML

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm generating XML data in a VB6 project (using MSXML3). Here's an example:

Dim oXDoc As MSXML2.DOMDocument30
Dim oXRoot As MSXML2.IXMLDOMNode
Dim oXNodeLevel2 As MSXML2.IXMLDOMNode
Dim oXNodeLevel3 As MSXML2.IXMLDOMNode
Dim oXNodeLevel4 As MSXML2.IXMLDOMNode

Set oXDoc = New MSXML2.DOMDocument30
Set oXRoot = oXDoc.appendChild(oXDoc.createNode(NODE_ELEMENT, "Root", ""))

Set oXNodeLevel2 = oXRoot.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level2", ""))
Set oXNodeLevel4 = oXNodeLevel2.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level4", ""))
oXNodeLevel4.Text = "Level4 Text"
Set oXNodeLevel3 = oXNodeLevel2.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level3", ""))
Set oXNodeLevel4 = oXNodeLevel3.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level4", ""))
oXNodeLevel4.Text = "Level4 Text"

oXDoc.save "z:\test.xml"

End Sub

This works fine, except the oXDoc.save method writes out the XML all on one line, no line breaks or indents.

I'd like this to be easier for humans to read, i.e. on separate lines, with each sub-level indented.

What's the best way to do this?

Walking through the oXDoc object and piecing together text is not an option.

If the best way is to apply an XSL transform, can you give me an example of what that would look like?

TIA

6 replies

by wjlarue

I would personally advise against doing this for two reasons: (1) XML files can get quite large and adding ?tabs? and ?returns? will just bloat the file even more, (2) there are many XML viewers that will walk the doc and display the XML in nice form. I like to use Internet Explorer to look at raw XML (just type in z:\test.xml as the address).

I do remember seeing some code a while ago that actually did produce indented XML files. I could try to hunt this code down for you if you really are set on doing it.

XSL is a data-driven means of modifying your XML document for viewing. It is a powerful transformation and styling language that can use pattern matching to conditionally apply different formatting to different elements. Most individuals would rather not see the XML tags so you would use XSL to format the XML into a pleasant, and much more readable, formatted table. This is only an example; there are unlimited amount things you can do with XSL, like ? applying a security model. XSL can transform on the server side allowing you to suppress private contact information, like SSN, from the XML that is sent to the client if the proper user requirements are not met.

Hope this helps.

Jeff
Raging Technologies

by andrew

Thanks Jeff, I appreciate the input.

I've been using XML/XSL for years now, and am well aware of the 'bloat factor'. My current app is a COM+ utility that's dumps a machine's metabase (COM component registrations) to an XML file, so I know in advance that it won't be large.

As such, the XML is metadata, and not ultimately intended for display. But during dev and debugging, it's nice to see the data.

I use XML notepad and IE, and , yes, they both format the XML nicely, but I need to do this from within my app, not an external process.

Tx again,

/Andrew

by wjlarue

I have added additional lines of code to your example that shows the use of an XSLT document to transform an XML document. I have tested this once again to make sure that it works. Once I read your e-mail, I too realized that I had over-looked a very obvious solution. My test application was just a single VB form with a WebBrowser object and a command button. The transformNodeToObject is the function that transforms the XML use XSL, which may be moot because of the WebBrowser object. Just like IE your can right click on the object and view source to see the indents.

Here is the modified sample code and I will post the XSL doc in a comment:

Dim oXDoc As MSXML2.DOMDocument30
Dim oXRoot As MSXML2.IXMLDOMNode
Dim oXNodeLevel2 As MSXML2.IXMLDOMNode
Dim oXNodeLevel3 As MSXML2.IXMLDOMNode
Dim oXNodeLevel4 As MSXML2.IXMLDOMNode

'Added next 5 lines of code
Dim xmlDocument As MSXML2.DOMDocument30
Dim xslTemplate As MSXML2.DOMDocument30
Set xmlDocument = New MSXML2.DOMDocument30Set xslTemplate = New MSXML2.DOMDocument30
xslTemplate.Load "c:\transform.xsl"

Set oXDoc = New MSXML2.DOMDocument30
Set oXRoot = oXDoc.appendChild(oXDoc.createNode(NODE_ELEMENT, "Root", ""))

Set oXNodeLevel2 = oXRoot.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level2", ""))
Set oXNodeLevel4 = oXNodeLevel2.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level4", ""))
oXNodeLevel4.Text = "Level4 Text"
Set oXNodeLevel3 = oXNodeLevel2.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level3", ""))
Set oXNodeLevel4 = oXNodeLevel3.appendChild(oXDoc.createNode(NODE_ELEMENT, "Level4", ""))
oXNodeLevel4.Text = "Level4 Text"

oXDoc.save "c:\test.xml"

'Added next three lines of code
oXDoc.transformNodeToObject xslTemplate, xmlDocument
xmlDocument.save "c:\NewTest.xml"
WebBrowser1.Navigate "c:\NewTest.xml"

by wjlarue

Here is the style sheet that I used with the transformNodeToObject function.

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method='xml' version='1.0' omit-xml-declaration='yes' indent='yes' />
<xsl:template match='/'>
<xsl:apply-templates select='*'/>
</xsl:template>
<xsl:template match='@* | * | text()'>
<xsl:copy><xsl:apply-templates select='@* | * | text()' /></xsl:copy>
</xsl:template>
</xsl:stylesheet>

I hope this helps you. If you do have any questions about any of this please feel free to contact me.

Jeff LaRue
Raging Technologies

This discussion is closed to new replies.