Results 1 to 11 of 11
  1. #1
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default CSV2XML java converter without packages?

    i found this online it looks correct but i cant seem to get it to work. If i comment out that whole try section ("System.out.println("Usage: java CSV2XML <inputCSVFile> <outputXMLFile>");") i receive an array out of bounds.
    but with this code it jumps into this try and i receive "Usage: java CSV2XML <inputCSVFile> <outputXMLFile>" I think it may be how i have created the input and output folders as these are the only things ive really changed.
    Java Code:
     
    package newpackage;
    
    
    import java.io.*; 
    import java.util.*; 
    import org.w3c.dom.*; 
    import javax.xml.parsers.*; 
    import javax.xml.transform.*; 
    import javax.xml.transform.dom.DOMSource; 
    import javax.xml.transform.stream.StreamResult; 
    
    public class CSV2XML { 
    
        // Protected Properties 
        protected DocumentBuilderFactory domFactory = null; 
        protected DocumentBuilder domBuilder = null; 
    
        // CTOR 
        public CSV2XML() 
        { 
            try 
            { 
                domFactory = DocumentBuilderFactory.newInstance(); 
                domBuilder = domFactory.newDocumentBuilder(); 
            } 
            catch(FactoryConfigurationError exp) 
            { 
                System.err.println(exp.toString()); 
            } 
            catch(ParserConfigurationException exp) 
            { 
                System.err.println(exp.toString()); 
            } 
            catch(Exception exp) 
            { 
                System.err.println(exp.toString()); 
            } 
        } 
    
        public int convertFile(String csvFileName, String xmlFileName) 
        { 
            int rowsCount = -1; 
            try 
            { 
                Document newDoc = domBuilder.newDocument(); 
    
                // Root element 
                Element rootElement = newDoc.createElement("CSV2XML"); 
                newDoc.appendChild(rootElement); 
    
                // Read comma seperated file 
                BufferedReader csvReader; 
                csvFileName = "C:\\ryan0j3\\test.csv"; 
                csvReader = new BufferedReader(new FileReader(csvFileName)); 
                int fieldCount = 0; 
                String[] csvFields = null; 
                StringTokenizer stringTokenizer = null; 
    
                // Assumption: first line in CSV file is column/field names 
                // As the column names are used to name the elements in the XML file, 
                // avoid using spaces/any other characters not suitable for XML element naming 
                String curLine = csvReader.readLine(); 
                if(curLine != null) 
                { 
                    stringTokenizer = new StringTokenizer(curLine, ","); 
                    fieldCount = stringTokenizer.countTokens(); 
                    if(fieldCount > 0) 
                    { 
                        csvFields = new String[fieldCount]; 
                        int i=0; 
                        while(stringTokenizer.hasMoreElements()) 
                            csvFields[i++] = String.valueOf(stringTokenizer.nextElement()); 
                    } 
                } 
    
                // Now we know the columns, now read data row lines 
                while((curLine = csvReader.readLine()) != null) 
                { 
                    stringTokenizer = new StringTokenizer(curLine, ","); 
                    fieldCount = stringTokenizer.countTokens(); 
    
                    if(fieldCount > 0) 
                    { 
                        Element rowElement = newDoc.createElement("row"); 
    
                        int i=0; 
                        while(stringTokenizer.hasMoreElements()) 
                        { 
                            try 
                            { 
                                String curValue = String.valueOf(stringTokenizer.nextElement()); 
                                Element curElement = newDoc.createElement(csvFields[i++]); 
                                curElement.appendChild(newDoc.createTextNode(curValue)); 
                                rowElement.appendChild(curElement); 
                            } 
                            catch(Exception exp) 
                            { 
                            } 
                        } 
    
                        rootElement.appendChild(rowElement); 
                        rowsCount++; 
                    } 
    
                } 
    
                csvReader.close(); 
    
                // Save the document to the disk file 
                TransformerFactory tranFactory = TransformerFactory.newInstance(); 
                Transformer aTransformer = tranFactory.newTransformer(); 
    
                Source src = new DOMSource(newDoc);
                xmlFileName = "C:\\ryan0j3\\test.xml";
                Result dest = new StreamResult(new File(xmlFileName)); 
                aTransformer.transform(src, dest); 
    
                rowsCount++; 
            } 
            catch(IOException exp) 
            { 
                System.err.println(exp.toString()); 
            } 
            catch(Exception exp) 
            { 
                System.err.println(exp.toString()); 
            } 
    
            return rowsCount; 
        } 
    
        public static void main(String[] args) 
        { 
            try 
            { 
                if(args.length != 2) 
                { 
                    System.out.println("Usage: java CSV2XML <inputCSVFile> <outputXMLFile>"); 
                    return; 
                } 
            } 
            catch(Exception exp) 
            { 
                System.err.println(exp.toString()); 
            } 
    
            try 
            { 
                CSV2XML csvConverter = new CSV2XML(); 
                int rowsCount = csvConverter.convertFile(args[0], args[1]); 
                 
                if(rowsCount >= 0) 
                { 
                    System.out.println("CSV File " + args[0] + 
                            "' successfully converted to XML File "+ args[1] + "\n" + 
                            "(" + String.valueOf(rowsCount) + " rows)"); 
                } 
                else 
                { 
                    System.out.println("Error while converting input CSV File " + args[0] + 
                            " to output XML File "+ args[1] + " "); 
                } 
            } 
            catch(Exception exp) 
            { 
                System.err.println(exp.toString()); 
            } 
        } 
    }
    Last edited by j0rdan; 04-23-2012 at 01:42 PM.

  2. #2
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: CSV2XML java converter without packages?

    month,year,value
    january,1990,30000
    july,1991,70000
    january,1992,120000
    july,1993,163333
    january,1994,208333
    july,1995,253333
    january,1996,298333
    july,1997,343333
    january,1998,388333
    july,1999,433333

    this is my csv file.

    Also this is the error code if i comment out the section that its going to.
    java.lang.ArrayIndexOutOfBoundsException: 0

    I assumed it was something to do with the way im trying to input/output the file as that was the only thing i seemed to change. Any help would be muchly appreciated or even a push int he right direction.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: CSV2XML java converter without packages?

    How are you running this?
    Are you running this the way that message says?
    java CSV2XML <inputCSVFile> <outputXMLFile>
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: CSV2XML java converter without packages?

    If you want to hardcode the names of the files in the program, add something like this after line 135:
    args = new String[] {"input filename here", "output filename here"}; // define files in program
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: CSV2XML java converter without packages?

    line 54 and line 115 is were i tried specifying the file.
    I'm just running it in netbeans and also tried eclipse.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: CSV2XML java converter without packages?

    Did it work when you coded it your way?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: CSV2XML java converter without packages?

    nope, I'm not quite sure what you meant with your post either as its already used the input file and read and wrote the file by line 115. line 115 is just used to saved the file as a xml unless I completely have the wrong impression of this code.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: CSV2XML java converter without packages?

    Java Code:
    int rowsCount = csvConverter.convertFile(args[0], args[1]);
    You want to either do as Norm suggests, which will create an args[] where args[0] is the input file and args[1] is the output file, or simply replace that call with this, defining the two files in the call:
    Java Code:
    int rowsCount = csvConverter.convertFile("<input file>", "<output file>");
    Last edited by Tolls; 04-23-2012 at 03:56 PM. Reason: Oops typo.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: CSV2XML java converter without packages?

    Thanks norm it worked great, so line 54 and 115 are not need ?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: CSV2XML java converter without packages?

    My code gives the program some filenames to use and makes it easier to quickly change where the filenames are specified. The other changes bury the changes in the code which can make for harder maintenance.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: CSV2XML java converter without packages?

    Yer i assumed it was something todo with were my input and output was i removed mine on the lines i said, It still works fine I sort of understand how it works I been trying to work it out for a couple of days now. Thanks alot.

Similar Threads

  1. Replies: 0
    Last Post: 11-05-2011, 07:16 AM
  2. Java Packages
    By ShitalJain in forum New To Java
    Replies: 12
    Last Post: 06-11-2011, 09:14 AM
  3. How to find java packages
    By h.ghorashi in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 07:03 PM
  4. pascal to java converter
    By j2me64 in forum Forum Lobby
    Replies: 1
    Last Post: 04-17-2010, 10:56 PM
  5. Replies: 3
    Last Post: 02-12-2008, 12:03 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
  •