VB 6.0 XML Output File Not Containing line feed character

Posted on 2003-11-24
Last Modified: 2008-09-26
I have written an application that opens an existing XML file. My software is written in VB 6.0 I am able to read the xml file, perform my needed tasks and then write my changes to the xml file. The error that I keep getting is when I append new children to the xml file I don't think I am getting any line feed characters to the xml. When I open the changed xml file with notepad I get changes that look like this

Existing XML file....
More Existing XML file...

What I want is output that look like this

I am using MSXML2.DOMDocument40 and I am attaching my nodes with appendChild
Question by:sld9t
LVL 14

Accepted Solution

waty earned 500 total points
ID: 9830972
' #Mandix Repository#*****************************************************
' * Programmer Name  : J R
' * Web Site         :
' * E-Mail           :
' * Date             : 06/07/2001
' **********************************************************************
' * Comments         : Format XML it with indented nodes
' *
' * This simple shunk of code uses MSXML 3.0 to take any well-formed
' * XML file specified on the command line and format it with indented
' * nodes. Always creates paired tags, even for empty nodes, with
' * the exception of text and CDATA nodes.
' *
' **********************************************************************

' MSXML3 node type constants used in code
Const NODE_TEXT = 3
' XML indentation formatting program
' Send any questions about this code to:
' and I'll try to get back to you as quick as I can
' Make sure we don't use anything we didn't declare
Option Explicit
' MSXML3 node type constants used in code
Const NODE_TEXT = 3
Public Sub Main()
   Dim bLoaded          As Boolean
   Dim strFileName      As String
   Dim strDTDFileName   As String
   Dim strStandalone    As String
   Dim xmlDoc           As New DOMDocument
   Dim nodeRoot         As IXMLDOMNode
   Dim strRootNodeName  As String
   Dim fsoFileSys       As New FileSystemObject
   Dim tstrXMLOut       As TextStream
   Dim strXMLOut        As String
   'this is not a robust way to get the command line,
   'support for quotation marks should be added if you care,
   'something else to add is an optional second parameter for the DTD filename
   strFileName = Trim(Command)
   'attempt to load the file into the XML DOM document
   bLoaded = xmlDoc.Load(strFileName)
   If bLoaded Then
      Set tstrXMLOut = fsoFileSys.OpenTextFile(strFileName, ForWriting, True)
      Set nodeRoot = xmlDoc.documentElement
      strRootNodeName = nodeRoot.nodeName
      'get the formatted XML string
      strXMLOut = FormatNodeAsString(nodeRoot)
      Set nodeRoot = Nothing
      Set xmlDoc = Nothing
      strStandalone = "no"
      'code assumes here that there is a DTD file of the same base name as the actual XML file
      strDTDFileName = Left(strFileName, Len(strFileName) - 4) & ".dtd"
      'build the necessary lines for any validXML file
      tstrXMLOut.WriteLine "<!-- <?xml version=" & """" & "1.0" & """" & " standalone=" & """" & strStandalone & """" & "?> -->"
      tstrXMLOut.WriteLine "<!-- <!DOCTYPE " & strRootNodeName & " SYSTEM " & """" & strDTDFileName & """" & "> -->"
      'write out the newly formatted XML string
      tstrXMLOut.Write strXMLOut
      'more cleanup
      Set tstrXMLOut = Nothing
      Set fsoFileSys = Nothing
      'oh goody, we're done
      MsgBox "Success. " & strFileName & " has been formatted.", , "Format Complete"
      MsgBox "Couldn't load the XML file: " & strFileName & vbCrLf & "I quit!", , "File Error"
   End If
End Sub

' This function recurses through the XMLtree given in the nodeIn parameter
' generally this would be the root node,at least that's how I call it but
' it would work to format any subtree aswell
Private Function FormatNodeAsString(nodeIn As IXMLDOMNode, Optional intIndent As Integer = 0) As String
   Dim I                As Integer
   Dim strXML           As String
   Dim bChildIsTextOrCDATA As Boolean
   Dim intDent          As Integer
   intDent = intIndent
   Dim nodemapAttributes As IXMLDOMNamedNodeMap
   Dim nodelistChildren As IXMLDOMNodeList
   Dim nodeTempChild    As IXMLDOMNode
   Dim lngNodeType      As Long
   lngNodeType = nodeIn.nodeType
   Dim strNodeVal       As String
   strNodeVal = nodeIn.nodeTypedValue
   Dim bHasChildren     As Boolean
   If lngNodeType = NODE_CDATA_SECTION Then
      strXML = strXML & "<![CDATA["
   ElseIf lngNodeType = NODE_TEXT Then
      'do nothing
      For I = 1 To intDent
         strXML = strXML & vbTab
      Next I
      strXML = strXML & "<" & nodeIn.nodeName
   End If
   Set nodemapAttributes = nodeIn.Attributes
   If Not (nodemapAttributes Is Nothing) Then
      For I = 0 To nodemapAttributes.length - 1
         Set nodeTempChild = nodemapAttributes.Item(I)
         strXML = strXML & " " & nodeTempChild.nodeName & "=" & """" & nodeTempChild.nodeTypedValue & """"
      Next I
   End If
   Set nodemapAttributes = Nothing
   If (lngNodeType <> NODE_CDATA_SECTION) And (lngNodeType <> NODE_TEXT) Then
      strXML = strXML & ">"
   End If
   Set nodelistChildren = nodeIn.childNodes
   bHasChildren = False
   If nodelistChildren.length > 0 Then
      bHasChildren = True
      Set nodeTempChild = nodelistChildren.Item(0)
      If Not ((nodeTempChild.nodeType = NODE_CDATA_SECTION) Or (nodeTempChild.nodeType = NODE_TEXT)) Then
         strXML = strXML & vbCrLf
         bChildIsTextOrCDATA = False
         bChildIsTextOrCDATA = True
      End If
      Do Until nodeTempChild Is Nothing
         strXML = strXML & FormatNodeAsString(nodeTempChild, intDent + 1)
         Set nodeTempChild = nodeTempChild.nextSibling
      strXML = strXML & strNodeVal
   End If
   Set nodelistChildren = Nothing
   Set nodeTempChild = Nothing
   If lngNodeType = NODE_CDATA_SECTION Then
      FormatNodeAsString = strXML & "]]>"
   ElseIf lngNodeType = NODE_TEXT Then
      FormatNodeAsString = strXML
      If bHasChildren And (Not bChildIsTextOrCDATA) Then
         For I = 1 To intDent
            strXML = strXML & vbTab
         Next I
      End If
      FormatNodeAsString = strXML & "</" & nodeIn.nodeName & ">" & vbCrLf
   End If
End Function

Author Comment

ID: 9842559
Thanks for the ideas J R. Looking over your comments, you are suggesting reprocessing the final xml and restructuring it as an external file. I would like to avoid this if possible I see it being inefficient. I hope that MS has some method built into their xml controls that would allow you to save the xml file with indentation and line feed characters.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now