Text file to XML file

Discussion in 'XML' started by Rizwan, Jul 30, 2004.

  1. Rizwan

    Rizwan Guest

    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
    Rizwan, Jul 30, 2004
    #1
    1. Advertising

  2. Rizwan

    Andy Dingley Guest

    On Fri, 30 Jul 2004 11:55:04 -0400, "Rizwan" <>
    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
    Andy Dingley, Jul 30, 2004
    #2
    1. Advertising

  3. Rizwan

    Graham Shaw Guest

    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>


    "Rizwan" <> wrote in message
    news:rbuOc.8728$...
    > 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
    >
    >
    >
    Graham Shaw, Jul 30, 2004
    #3
  4. 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/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
    Gerald Aichholzer, Jul 30, 2004
    #4
  5. "Rizwan" <> wrote in message
    news:rbuOc.8728$...
    > 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
    Daniel Parker, Jul 31, 2004
    #5
  6. "Daniel Parker" <danielaparker@spam?nothanks.windupbird.com> wrote in
    message news:qZAOc.14863$...
    > "Rizwan" <> wrote in message
    > news:rbuOc.8728$...
    > > 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
    Daniel Parker, Jul 31, 2004
    #6
  7. In article <rbuOc.8728$>,
    Rizwan <> 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
    Patrick TJ McPhee, Aug 2, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jkflens
    Replies:
    2
    Views:
    1,449
    jkflens
    May 30, 2006
  2. Erik Wasser
    Replies:
    5
    Views:
    437
    Peter J. Holzer
    Mar 5, 2006
  3. Bjoern Hoehrmann
    Replies:
    3
    Views:
    313
    Bart Van der Donck
    Jun 12, 2008
  4. Chris Angelico
    Replies:
    9
    Views:
    226
    Andrew Cooper
    Jul 29, 2012
  5. Tim Chase
    Replies:
    10
    Views:
    366
    Robert Miles
    Aug 31, 2012
Loading...

Share This Page