Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse file to XML

Status
Not open for further replies.

tnayfeh

Programmer
Apr 21, 2004
39
CA
I am very new to the XML classes and i'm having difficulty adding sections to the existing code I have below. This fuction parses a text file into a simple XML file. Say I wanted to add another CollectionName element to this file before the existing one, how can I do that? Also, if I wanted to concatenate the child members on one line, can that be done?

Thanks in advance!
TN

Code:
Public Function Text2XML(textFilePath As String, objectName As String, _
    collectionName As String, Optional delimiter As String = ":")
    
    Const ForReading = 1
    
    Dim fs As Object
    Dim ts As Object
    Dim line As String
    Dim xmlDoc As DOMDocument
    Dim usersDoc As DOMDocument
    Dim userDoc As DOMDocument
    Dim userNode As IXMLDOMElement
    Dim fields As Variant
    Dim field As Variant
    Dim fieldValues As Variant
    Dim fieldValue As Variant
    Dim tempNode As IXMLDOMElement
    Dim fieldName As String
    Dim index As Long
    Dim txtfieldvalue As String

    ' Create a collection object (usersDoc) and a user instance (userDoc)
    Set usersDoc = CreateObject("Microsoft.XMLDOM")
    Set userDoc = CreateObject("Microsoft.XMLDOM")
    
    Set usersDoc.documentElement = usersDoc.createElement(collectionName)
    Set userDoc.documentElement = usersDoc.createElement(objectName)

    ' Open the Common Value Separated (CVS) text file
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.opentextfile(textFilePath, ForReading, False)
       ' Read the first line to get the header names and put
    ' into variant array(with split)
    line = ts.ReadLine
    fields = Split(line, ":")

    ' Create a user template with one node for each field
    For Each field In fields
        Set tempNode = userDoc.createElement(field)
        userDoc.documentElement.appendChild tempNode
    Next

    ' Iterate through each line of the text stream:
    While Not ts.AtEndOfStream
        'Read the next line in the text stream
        line = ts.ReadLine
        ' Get the values as a variant array (setting it first to a blank
        ' string to "unlock" the previous array, if set).
        fieldValues = ""
        fieldValues = Split(line, ":")
        
        cnt = UBound(fieldValues)
        If cnt >= 3 Then
        
        ' Create a copy of the user template
        Set userNode = userDoc.documentElement.cloneNode(True)
        ' Iterate through each field
        For index = 0 To UBound(fieldValues)
            field = fields(index)
            If index = 1 Then
             fieldValue = Split(fieldValues(1), "\")
             fieldValue = fieldValue(1)
            Else
             fieldValue = fieldValues(index)
            End If
            ' and set the text for the node corresponding to the field name
            userNode.selectSingleNode(field).Text = CStr(fieldValue)
        Next
        ' Add the child to the usersDoc collection
        usersDoc.documentElement.appendChild userNode
        End If
    Wend
    ' and output usersDoc
    Set Text2XML = usersDoc

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top