vb script read in a file and reference a line

Hi

How can i read the contents of a text file and reference a particular word?

So for example

TX file contains a list

Description     Date       Occ
Test1               12.01.15   2
Test2               14.04.15   3

I want to be able to split these lines so i can then put them into XML tags (VB will also create in the same script)
<Message>
 <Log>  
       <Desc>Test1<Desc>
       <Date>12.01.15<Date>
       <Occ>2<Occ>
 </Log>
 <Log>
     <Desc>Test2<Desc>
     <Date>14.04.15<Date>
     <Occ>3<Occ>
 </Log>
</Message>


Thanks
wilko1000Asked:
Who is Participating?
 
Jeff DarlingDeveloper AnalystCommented:
Dim strXMLFile,strTXTFile,strXSLFile

strTXTFile = "e:\today\03\data.txt"
strXMLFile = "e:\today\03\data.xml"
strXSLFile = "e:\today\03\Indent.xsl"


' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
' Setup XML Object
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Dim objRoot,xmlDoc,objRecord,objName,objDate,objOcc

Set xmlDoc = CreateObject("Msxml2.DOMDocument")   
Set objRoot = xmlDoc.createElement("Message")  
xmlDoc.appendChild objRoot  

' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
' Read Text file
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strTXTFile, ForReading)

'Skip Header
strNextLine = objTextFile.ReadLine

Do Until objTextFile.AtEndOfStream

    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' Read Record
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  

    strNextLine = objTextFile.ReadLine
    
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' Populate fields
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    
    
    strDesc = Trim(Mid(strNextline,1,20))
    strDate = Trim(Mid(strNextline,21,8))
    strOCC  = Trim(Mid(strNextline,32,8))
    
    Set objRecord = xmlDoc.createElement("Log") 
    objRoot.appendChild objRecord 
    
    Set objName = xmlDoc.createElement("Desc")  
    objName.Text = strDesc
    objRecord.appendChild objName  
    
    Set objDate = xmlDoc.createElement("Date")  
    objDate.Text = strDate  
    objRecord.appendChild objDate  
    
    Set objOcc = xmlDoc.createElement("Occ")  
    objOcc.Text = strOCC  
    objRecord.appendChild objOcc 
    
    
Loop

xmlDoc.Save strXMLFile

' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
' Perform XSL Transform for indenting
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Set xsl = CreateObject("Msxml2.DOMDocument")
xsl.async = False
xsl.load(strXSLFile)

strXML = xmlDoc.transformNode(xsl)

xmlDoc.loadXML(strXML)

If (xmlDoc.firstChild.nodeType = 7) And (xmlDoc.firstChild.nodeName = "xml") Then
    Set child = xmlDoc.firstChild
    xmlDoc.removeChild child
End If

Set xmlTemp = xmlDoc.createProcessingInstruction("xml","version='1.0' encoding='utf-8'")
xmlDoc.insertBefore xmlTemp, xmlDoc.firstChild

xmlDoc.Save strXMLFile

Open in new window


XSL for indenting

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" version="1.0">
     <xsl:output method="xml" encoding="UTF-8" indent="yes" />
     <!--Important!  Remove existing whitespace in DOM elements.-->
     <xsl:strip-space elements="*"/>
     <!--Identity transformation (see http://www.w3.org/TR/xslt#copying).-->
     <xsl:template match="@*|node()">
         <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
     </xsl:template>
</xsl:stylesheet> 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.