Solved

VB 6.0 XML Output File Not Containing line feed character

Posted on 2003-11-24
3
506 Views
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....
                <parentNodeIamAttachingTo>
                    <existingChild>ExistingData</existingChild>
                    <existingChild2>ExistingData</existingChild2>
                    <myNewChild>MyNewData</myNewChild><myNewChildsChild>itsData</myNewChildsChild><myNewChild2>myNewData</MyNewChild2></parentNodeIamAttachingto>
More Existing XML file...

What I want is output that look like this
                <parentNodeIamAttachingTo>
                    <existingChild>ExistingData</existingChild>
                    <existingChild2>ExistingData</existingChild2>
                    <myNewChild>MyNewData</myNewChild>
                        <myNewChildsChild>itsData</myNewChildsChild>
                    <myNewChild2>myNewData</MyNewChild2>
                </parentNodeIamAttachingto>

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

Accepted Solution

by:
waty earned 500 total points
ID: 9830972
' #Mandix Repository#*****************************************************
' * Programmer Name  : J R
' * Web Site         : http://www.vbdiamond.com
' * E-Mail           : vb_questions@swbell.net
' * 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
Const NODE_CDATA_SECTION = 4
'---------------------------------------------------------------------
'---------------------------------------------------------------------
' XML indentation formatting program
'---------------------------------------------------------------------
' Send any questions about this code to:
' vb_questions@swbell.net
' 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
Const NODE_CDATA_SECTION = 4
'---------------------------------------------------------------------
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)
      'cleanup
      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
      tstrXMLOut.Close
      'more cleanup
      Set tstrXMLOut = Nothing
      Set fsoFileSys = Nothing
      'oh goody, we're done
      MsgBox "Success. " & strFileName & " has been formatted.", , "Format Complete"
   Else
      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
   Else
      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
      Else
         bChildIsTextOrCDATA = True
      End If
      Do Until nodeTempChild Is Nothing
         strXML = strXML & FormatNodeAsString(nodeTempChild, intDent + 1)
         Set nodeTempChild = nodeTempChild.nextSibling
      Loop
   Else
      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
   Else
      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
0
 

Author Comment

by:sld9t
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 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

13 Experts available now in Live!

Get 1:1 Help Now