Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Text file to XML file

Reply
Thread Tools

Text file to XML file

 
 
Rizwan
Guest
Posts: n/a
 
      07-30-2004
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



 
Reply With Quote
 
 
 
 
Andy Dingley
Guest
Posts: n/a
 
      07-30-2004
On Fri, 30 Jul 2004 11:55:04 -0400, "Rizwan" <(E-Mail Removed)>
wrote:

>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.

--
Smert' spamionam
 
Reply With Quote
 
 
 
 
Graham Shaw
Guest
Posts: n/a
 
      07-30-2004
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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput 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,"/>
</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>


"Rizwan" <(E-Mail Removed)> wrote in message
news:rbuOc.8728$(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Gerald Aichholzer
Guest
Posts: n/a
 
      07-30-2004
Rizwan wrote:

> 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/developerwork...ataxslt-i.html
(Analyze non-XML data with XSLT)

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

HTH,
Gerald
 
Reply With Quote
 
Daniel Parker
Guest
Posts: n/a
 
      07-30-2004
"Rizwan" <(E-Mail Removed)> wrote in message
news:rbuOc.8728$(E-Mail Removed)...
> I have the following tasks :
> 1) receive a flat text file which contains data and
> 2) convert it into an XML
>


You might want to have a look at the examples link for the Serving XML open
source project,

https://sourceforge.net/projects/servingxml/

Your example would fall into the "easy" category.

Regards,
Daniel Parker



 
Reply With Quote
 
Daniel Parker
Guest
Posts: n/a
 
      07-30-2004
"Daniel Parker" <danielaparker@spam?nothanks.windupbird.com> wrote in
message news:qZAOc.14863$(E-Mail Removed).. .
> "Rizwan" <(E-Mail Removed)> wrote in message
> news:rbuOc.8728$(E-Mail Removed)...
> > I have the following tasks :
> > 1) receive a flat text file which contains data and
> > 2) convert it into an XML
> >

>
> You might want to have a look at the examples link for the Serving XML

open
> source project,
>

Sorry, should have given you this link

http://servingxml.sourceforge.net/

Daniel


 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      08-02-2004
In article <rbuOc.8728$(E-Mail Removed)>,
Rizwan <(E-Mail Removed)> wrote:

% 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.
--

Patrick TJ McPhee
East York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: OT: Text editors (was Re: Search and replace text in XML file?) Tim Chase Python 10 08-31-2012 03:56 AM
OT: Text editors (was Re: Search and replace text in XML file?) Chris Angelico Python 9 07-29-2012 05:55 PM
Should the webserver add "Content-Type: text/xml header" to the requested XML file? Bjoern Hoehrmann Javascript 3 06-12-2008 08:05 AM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM



Advertisments