Text file to XML file

R

Rizwan

I have the following tasks :
1) receive a flat text file which contains data and
2) convert it into an XML


The flat text file for example is in this format :

1 2
1234567890123456789012345

where :
Column 1 : recordType
Column 2 to 8 : companyNumber
Column 9 to 14 : batchNumber
Column 15 to 22 : hoursTotal
Column 23 to 25 : userField

After processing, the XML I get will be in this format :

<records>
<record>
<recordType>B</recordType>
<companyNumber>1127897</companyNumber>
<batchNumber>001234</batchNumber>
<hoursTotal>24567809</hoursTotal>
<userField>A09</userField>
</record>
<record>
<recordType>B</recordType>
<companyNumber>1124183</companyNumber>
<batchNumber>091334</batchNumber>
<hoursTotal>94857809</hoursTotal>
<userField>A08</userField>
</record>
</records>


Now I am a newbie regarding XML, so I was hpoing if somebody can give me the
abstract steps involved for this process.

Thanks
 
A

Andy Dingley

Now I am a newbie regarding XML, so I was hpoing if somebody can give me the
abstract steps involved for this process.

1. Learn some simple every-purpose scripting language. You might
already know one - or Perl is always popular.

2. Learn some basics of XML (you seem to have managed this already).

3. Learn the basics of the "XML DOM" interface.

4. Learn how to connect to an XML DOM from your chosen scripting
language. Most can do this pretty easily - try asking around in the
language's support groups.

5. Write some code. Very simple code to loop over all the lines, split
each one up into fragments, error check it, then add some elements to
the XML DOM with the createNode() and appendChild() methods.
 
G

Graham Shaw

Rizwan,

I was going to suggest you just use VB to create it it peicemeal, but as I
too am a newbie to XML I thought I'd give you a mixed VB / XML / XSLT
solution

Hope you find it useful

Graham

VB to convert flat file to flat XML

Dim fso As New FileSystemObject
Dim ts As TextStream
Set ts = fso_OpenTextFile("c:\input.txt")
Dim strArray
'Take the whole contents and 'split it into an array of strings'
strArray = Split(ts.ReadAll, vbCrLf)
ts.Close
Dim XMLstr As String
'Join the elements of the array with "</record><record>" as the
delimeter
XMLstr = "<records><record>" & Join(strArray, "</record><record>") &
"</record></record>"
'you can then just save the string or do it via a DOMDocument (not
really necessay but hey.....)
Dim objXML As New MSXML2.DOMDocument40
objXML.loadXML XMLstr
objXML.save "C:\output.xml"

Then transform it with this XSLT

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="xml"/>
<xsl:template match="records">
<xsl:element name="records">
<xsl:for-each select="/records/record">
<xsl:element name="record">
<xsl:element name="recordType">
<xsl:value-of select="substring(.,1,1)"/>
</xsl:element>
<xsl:element name="companyNumber">
<xsl:value-of select="substring(.,2,7)"/>
</xsl:element>
<xsl:element name="batchNumber">
<xsl:value-of select="substring(.,8,6)"/>
</xsl:element>
<xsl:element name="hoursTotal">
<xsl:value-of select="substring(.,15,8)"/>
</xsl:element>
<xsl:element name="userField">
<xsl:value-of select="substring(.,23,3)"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 
G

Gerald Aichholzer

Rizwan said:
I have the following tasks :
1) receive a flat text file which contains data and
2) convert it into an XML


The flat text file for example is in this format :

1 2
1234567890123456789012345

where :
Column 1 : recordType
Column 2 to 8 : companyNumber
Column 9 to 14 : batchNumber
Column 15 to 22 : hoursTotal
Column 23 to 25 : userField

After processing, the XML I get will be in this format :

<records>
<record>
<recordType>B</recordType>
<companyNumber>1127897</companyNumber>
<batchNumber>001234</batchNumber>
<hoursTotal>24567809</hoursTotal>
<userField>A09</userField>
</record>
<record>
<recordType>B</recordType>
<companyNumber>1124183</companyNumber>
<batchNumber>091334</batchNumber>
<hoursTotal>94857809</hoursTotal>
<userField>A08</userField>
</record>
</records>


Now I am a newbie regarding XML, so I was hpoing if somebody can give me the
abstract steps involved for this process.

the following tutorial at IBM developerworks might give you some
hints about finishing your task:

http://www-106.ibm.com/developerworks/xml/edu/x-dw-xdataxslt-i.html
(Analyze non-XML data with XSLT)

You have to register for viewing, but it's free.

HTH,
Gerald
 
P

Patrick TJ McPhee

% I have the following tasks :
% 1) receive a flat text file which contains data and
% 2) convert it into an XML

I don't think there's any point in using specialised tools to
create XML. Reading it is different. Use a parser to read it. Don't
write your own parser unless you're interested in understanding all
the ins and outs and getting them right, but creating XML is close
to trivial.

The things you need to be careful of:

- character sets. You have to know what character set was used to
encode your input data and either declare the XML file to use that
data or convert it to UTF-8;
- < and &. Replace them with &lt; and &amp;.

Before you start, you ought to design the output format and document it
either by writing a DTD, a w3c schema or a RELAX NG schema. This will be
more work than creating the conversion program, but it will be helpful
in ensuring the conversion is valid XML.

There are some philosophical issues in the design. Should each record be
represented by an empty element with a bunch of attributes, or by an
element with a bunch of other elements containing CDATA for the data?
Should each record be a `record' element, or should I go for
interoperability by describing the type of record? Should I give my data
a name space or is this too obvious a question and I just left it out of
my example for the sake of brevity?

Except for the last one, you seem to have resolved these issues in a
satisfactory manner, so damn the torpedos and have fun.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top