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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.