Flat File to XML file output- Need help in the Code


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Flat File to XML file output- Need help in the Code

  1. #1
    Join Date
    Jan 2008
    Posts
    2

    Post Flat File to XML file output- Need help in the Code

    Hi,

    I need help in the below scenario .

    Input is Flat File
    output should be XML.

    Below is the JAVA Code :
    Code:
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.StringTokenizer;
    
    public class ConvertXML {
    public ArrayList readFile(String inputFile) throws Exception
    {
    String line = "";
    ArrayList list = new ArrayList();
    FileInputStream fin = new FileInputStream(inputFile);
    DataInputStream din = new DataInputStream(fin);
    while ((line = din.readLine()) != null)
    list.add(line); 
    return list;
    }
    /**
    * @param list
    * @return
    */
    public String formatList(ArrayList list)
    {
    
    String output="";
    String H1Start="<RECORDSET>";
    String H1End="</RECORDSET>";
    String H2Start="<RECORD>";
    String H2End="</RECORD>";
    String tempStr = "";
    
    //Getting the header values of each
    String strHeader=null;
    StringTokenizer strHeadTokens=null;
    List HeaderStartList=new ArrayList();
    strHeader=(String)list.get(0);
    strHeadTokens=new StringTokenizer(strHeader,"|");
    while(strHeadTokens.hasMoreTokens())
    { 
    HeaderStartList.add(strHeadTokens.nextToken());
    } 
    //Looping the Array List for each line
    for (int i = 1; i < list.size(); i++)
    {
    String strValues=null;
    StringTokenizer strValueTokens=null;
    
    strValues=(String)list.get(i);
    strValueTokens=new StringTokenizer(strValues,"|");
    int tokenSize=0;
    output+=H2Start+"\n";
    while(strValueTokens.hasMoreTokens())
    {
    /*if(strValueTokens.nextElement().equals(""))
    {
    System.out.println("Enterd into null values");
    //strValueTokens.equals("");
    }*/
    tempStr=(String)"<">"strValueTokens.nextToken().trim()"</">"+"\n";
    tokenSize++;
    //System.out.println(tempStr);
    output+= tempStr;
    }
    output=output+H2End+"\n";
    
    
    }
    output=H1Start+"\n"+output+H1End;
    
    return output;
    }
    
    public static void main(String[] args) throws FileNotFoundException {
    ConvertXML fj = new ConvertXML();
    ArrayList list = null ;
    try {//input file 
    list = fj.readFile("C:/Documents and Settings/Administrator/workspace/DXY2XI/com/DC/XI/utils/Mdplus.txt");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    String str = fj.formatList(list);
    
    //String className = fj.getClassName(list);
    //output file directory
    FileOutputStream fout =
    new FileOutputStream("C:/Documents and Settings/Administrator/workspace/DXY2XI/com/DC/XI/utils/Rama.xml");
    PrintWriter out = new PrintWriter(new OutputStreamWriter(fout));
    out.println(str);
    out.close();
    }
    
    
    }

    Input file structure :

    SPARTE|BM|ERWEITERUNG|CODETYP|CODE|GUELTIGAB|GUELTIGBIS|BLOCKFLAG_WS|BLOCKFLAG|MANUALORDER S|MARKETINGFLAG|
    0|16418622|AU1|0|205|5/1/2005|12/31/2155|0|0|||
    0|16418622|AU1|0|214|5/1/2005|12/31/2155|0|0|0|0|
    0|16418622|AU1|0|218|5/1/2005|12/31/2155|0|0|||
    0|16418622|AU1|0|219|5/1/2005|12/31/2155|0|0|||
    0|16418622|AU1|0|220|5/1/2005|12/31/2155|0|0|0|0|
    0|16418622|AU1|0|241|5/1/2005|12/31/2155|0|0|0|0|
    0|16418622|AU1|0|244|5/1/2005|12/31/2155|0|0|0|0|
    0|16418622|AU1|0|245|5/1/2005|12/31/2155|0|0|0|0|
    0|16418622|AU1|0|249|5/1/2005|12/31/2155|0|0|0|0|
    0|16418622|AU1|0|260|5/1/2005|8/31/2005|0|0|0|0|
    0|16418622|AU1|0|260|9/1/2005|12/31/2155|0|0|0|0|

    Output File structure:

    <RECORDSET>
    - <RECORD>
    <SPARTE>0</SPARTE>
    <BM>16418622</BM>
    <ERWEITERUNG>AU1</ERWEITERUNG>
    <CODETYP>0</CODETYP>
    <CODE>205</CODE>
    <GUELTIGAB>5/1/2005</GUELTIGAB>
    <GUELTIGBIS>12/31/2155</GUELTIGBIS>
    <BLOCKFLAG_WS>0</BLOCKFLAG_WS>
    <BLOCKFLAG>0</BLOCKFLAG>
    </RECORD>
    - <RECORD>
    <SPARTE>0</SPARTE>
    <BM>16418622</BM>
    <ERWEITERUNG>AU1</ERWEITERUNG>
    <CODETYP>0</CODETYP>
    <CODE>214</CODE>
    <GUELTIGAB>5/1/2005</GUELTIGAB>
    <GUELTIGBIS>12/31/2155</GUELTIGBIS>
    <BLOCKFLAG_WS>0</BLOCKFLAG_WS>
    <BLOCKFLAG>0</BLOCKFLAG>
    <MANUALORDERS>0</MANUALORDERS>

    <MARKETINGFLAG>0</MARKETINGFLAG>
    </RECORD>

    ..... Till end of the records.

    Issue : In the Output file, if any of the field does not have any value, then the tag should be created as empty.
    but the current output file is not creating those fields if there is no value.

    Ex: see the 2nd record in the output marked in bold, they are created as there is value in the input for the 2nd record. But if you observe in the first output record, those fields are not created as there is no value in the input.
    My concern is they should be created in the below format :
    <MANUALORDERS />
    <MARKETINGFLAG />

    Any help is highly appreciated.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    your parser should be able to test whether the field is empty ... and if so, create a self-closing tag and move to the next field

  3. #3
    Join Date
    Jan 2008
    Posts
    2
    Hi,

    StringTokenizer does not return empty tokens. The workaround is to include delimiters as part of tokens using the constructor StringTokenizer(string, delimiter, boolean includeDelimiter). While looping you can check whether the previous token and the curretn token equals to the delimiter used, if yes, it's an empty token and an empty element is included in the output.

    Something similar to this.

    Code:
    		while(tokenizer.hasMoreElements()) {			
    			token = tokenizer.nextToken();
    			if(delimiter.equals(token) && delimiter.equals(previousToken)) {				
    				System.out.println("<"+tags.get(counter)+"/>");
    				counter++;				
    			} else if(delimiter.equals(token)) {
    				//ignore
    			} else {
    				System.out.println("<"+tags.get(counter)+">");
    				System.out.println(token);
    				System.out.println("</"+tags.get(counter)+">");
    				counter++;
    			}
    			
    			previousToken = token;
    		}

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    This is one area where Scanner is better at the job than StringTokenizer. You can use the boolean hasNext() to test whether there is an entry; you can also test the possible datatype of the next token.

  5. #5
    Join Date
    Jul 2005
    Posts
    78
    I thought that the use of StringTokenizer is frowned upon...

    Java 6 API:
    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Similar Threads

  1. VS.Net and Code Behind
    By Chaitanya Marvici in forum ASP.NET
    Replies: 6
    Last Post: 07-21-2003, 10:15 AM
  2. Replies: 0
    Last Post: 06-04-2002, 02:30 AM
  3. .NET equals Efficiency
    By Kevin in forum .NET
    Replies: 150
    Last Post: 03-04-2002, 06:40 PM
  4. XML from Mainframe flat file
    By Gee in forum XML
    Replies: 11
    Last Post: 08-23-2001, 05:47 PM
  5. Replies: 0
    Last Post: 04-03-2001, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center