Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Generating XML form CSV  using java

Posted on 2009-05-08
8
Medium Priority
?
848 Views
Last Modified: 2013-11-13
Hi ,

I am generating the below xml for CSV using ricebridge.xmlman opensource api, can anybody please suggest any other free opensource api to do the same functionality.
Or if we can do with XSL please provide the XSL code.
Thanks.
XML:
 
<?xml version='1.0' encoding='UTF-8'?>
<ROWSET>
  <ROW>
    <EMPLOYEE_FULL_NAME>Lamprecht, Mr. Mike</EMPLOYEE_FULL_NAME>
    <ASSIGNMENT_NUMBER>201</ASSIGNMENT_NUMBER>
    <EMPLOYEE_NUMBER>201</EMPLOYEE_NUMBER>
    <EMPLOYMENT_CATEGORY>Fulltime-Regular</EMPLOYMENT_CATEGORY>
    <DATE_OF_HIRE>01-JAN-98</DATE_OF_HIRE>
    <GENDER>M</GENDER>
    <ASSIGNMENT_STATUS>Active Assignment</ASSIGNMENT_STATUS>
    <DATE_OF_BIRTH>09-MAY-65</DATE_OF_BIRTH>
    <JOB_NAME>VP300.Vice President</JOB_NAME>
    <MARITAL_STATUS>Married</MARITAL_STATUS>
    <SUPERVISOR_FULL_NAME>Andretti, Mr. Evan</SUPERVISOR_FULL_NAME>
    <PAYROLL_NAME>Vision HR Semi-Monthly</PAYROLL_NAME>
    <GRADE_NAME>100.1.South</GRADE_NAME>
    <ETHNIC_ORIGIN>White</ETHNIC_ORIGIN>
    <SOCIAL_SECURITY_NUMBER>367-98-3283</SOCIAL_SECURITY_NUMBER>
    <LOCATION_NAME>VA- Philadelphia</LOCATION_NAME>
    <GOVERNMENT_REPORTING_ENTITY>Vision Services</GOVERNMENT_REPORTING_ENTITY>
    <ORGANIZATION_NAME>Vision Services</ORGANIZATION_NAME>
    <PAY_BASIS>MONTHLY</PAY_BASIS>
    <PERSON_TYPE>Employee</PERSON_TYPE>
    <LATEST_HIRE_DATE>01-JAN-98</LATEST_HIRE_DATE>
    <ADDRESS_LINE1>983 Mainstream Ave</ADDRESS_LINE1>
    <ADDRESS_CITY>Atlanta</ADDRESS_CITY>
    <ADDRESS_STATE>Georgia</ADDRESS_STATE>
    <ADDRESS_ZIP>30010</ADDRESS_ZIP>
    <LENGTH_OF_SERVICE>11.35</LENGTH_OF_SERVICE>
  </ROW>
  <ROW>
    <EMPLOYEE_FULL_NAME>Thomas, Cindy</EMPLOYEE_FULL_NAME>
    <ASSIGNMENT_NUMBER>226</ASSIGNMENT_NUMBER>
    <EMPLOYEE_NUMBER>226</EMPLOYEE_NUMBER>
    <EMPLOYMENT_CATEGORY>Fulltime-Regular</EMPLOYMENT_CATEGORY>
    <DATE_OF_HIRE>01-JAN-99</DATE_OF_HIRE>
    <GENDER>F</GENDER>
    <ASSIGNMENT_STATUS>Active Assignment</ASSIGNMENT_STATUS>
    <DATE_OF_BIRTH>10-AUG-68</DATE_OF_BIRTH>
    <JOB_NAME></JOB_NAME>
    <MARITAL_STATUS>Married</MARITAL_STATUS>
    <SUPERVISOR_FULL_NAME></SUPERVISOR_FULL_NAME>
    <PAYROLL_NAME></PAYROLL_NAME>
    <GRADE_NAME></GRADE_NAME>
    <ETHNIC_ORIGIN>White</ETHNIC_ORIGIN>
    <SOCIAL_SECURITY_NUMBER>222-11-3333</SOCIAL_SECURITY_NUMBER>
    <LOCATION_NAME>VA- Philadelphia</LOCATION_NAME>
    <GOVERNMENT_REPORTING_ENTITY>Vision Services</GOVERNMENT_REPORTING_ENTITY>
    <ORGANIZATION_NAME>Vision Services</ORGANIZATION_NAME>
    <PAY_BASIS></PAY_BASIS>
    <PERSON_TYPE>Employee</PERSON_TYPE>
    <LATEST_HIRE_DATE>01-JAN-99</LATEST_HIRE_DATE>
    <ADDRESS_LINE1>4 Saw Mill Ct.</ADDRESS_LINE1>
    <ADDRESS_CITY>Philadelphia</ADDRESS_CITY>
    <ADDRESS_STATE>Pennsylvania</ADDRESS_STATE>
    <ADDRESS_ZIP>19038</ADDRESS_ZIP>
    <LENGTH_OF_SERVICE>10.35</LENGTH_OF_SERVICE>
  </ROW>
  <ROW>
    <EMPLOYEE_FULL_NAME>Che:n, Tony</EMPLOYEE_FULL_NAME>
    <ASSIGNMENT_NUMBER>227</ASSIGNMENT_NUMBER>
    <EMPLOYEE_NUMBER>227</EMPLOYEE_NUMBER>
    <EMPLOYMENT_CATEGORY>Fulltime-Regular</EMPLOYMENT_CATEGORY>
    <DATE_OF_HIRE>01-JAN-99</DATE_OF_HIRE>
    <GENDER>M</GENDER>
    <ASSIGNMENT_STATUS>Active Assignment</ASSIGNMENT_STATUS>
    <DATE_OF_BIRTH>28-SEP-63</DATE_OF_BIRTH>
    <JOB_NAME></JOB_NAME>
    <MARITAL_STATUS>Married</MARITAL_STATUS>
    <SUPERVISOR_FULL_NAME>Thomas, Cindy</SUPERVISOR_FULL_NAME>
    <PAYROLL_NAME></PAYROLL_NAME>
    <GRADE_NAME></GRADE_NAME>
    <ETHNIC_ORIGIN>White</ETHNIC_ORIGIN>
    <SOCIAL_SECURITY_NUMBER>149-21-9421</SOCIAL_SECURITY_NUMBER>
    <LOCATION_NAME>VA- Philadelphia</LOCATION_NAME>
    <GOVERNMENT_REPORTING_ENTITY>Vision Services</GOVERNMENT_REPORTING_ENTITY>
    <ORGANIZATION_NAME>Vision Services</ORGANIZATION_NAME>
    <PAY_BASIS></PAY_BASIS>
    <PERSON_TYPE>Employee</PERSON_TYPE>
    <LATEST_HIRE_DATE>01-JAN-99</LATEST_HIRE_DATE>
    <ADDRESS_LINE1>14 Broad St.</ADDRESS_LINE1>
    <ADDRESS_CITY>Philadelphia</ADDRESS_CITY>
    <ADDRESS_STATE>Pennsylvania</ADDRESS_STATE>
    <ADDRESS_ZIP>19092</ADDRESS_ZIP>
    <LENGTH_OF_SERVICE>10.35</LENGTH_OF_SERVICE>
  </ROW>
  <ROW>
    <EMPLOYEE_FULL_NAME>Noe, Renee</EMPLOYEE_FULL_NAME>
    <ASSIGNMENT_NUMBER>228</ASSIGNMENT_NUMBER>
    <EMPLOYEE_NUMBER>228</EMPLOYEE_NUMBER>
    <EMPLOYMENT_CATEGORY>Fulltime-Regular</EMPLOYMENT_CATEGORY>
    <DATE_OF_HIRE>01-JAN-99</DATE_OF_HIRE>
    <GENDER>F</GENDER>
    <ASSIGNMENT_STATUS>Active Assignment</ASSIGNMENT_STATUS>
    <DATE_OF_BIRTH>12-FEB-68</DATE_OF_BIRTH>
    <JOB_NAME>MGR500.Manager</JOB_NAME>
    <MARITAL_STATUS>Married</MARITAL_STATUS>
    <SUPERVISOR_FULL_NAME>Ang, Ms. Lay Kuan</SUPERVISOR_FULL_NAME>
    <PAYROLL_NAME>Vision Monthly</PAYROLL_NAME>
    <GRADE_NAME></GRADE_NAME>
    <ETHNIC_ORIGIN>Black or African American</ETHNIC_ORIGIN>
    <SOCIAL_SECURITY_NUMBER>214-19-4219</SOCIAL_SECURITY_NUMBER>
    <LOCATION_NAME>VS- Washington DC</LOCATION_NAME>
    <GOVERNMENT_REPORTING_ENTITY>Vision Operations</GOVERNMENT_REPORTING_ENTITY>
    <ORGANIZATION_NAME>Vision Administration</ORGANIZATION_NAME>
    <PAY_BASIS>MONTHLY</PAY_BASIS>
    <PERSON_TYPE>Employee</PERSON_TYPE>
    <LATEST_HIRE_DATE>01-JAN-99</LATEST_HIRE_DATE>
    <ADDRESS_LINE1>17 Chestnut St.</ADDRESS_LINE1>
    <ADDRESS_CITY>Philadelphia</ADDRESS_CITY>
    <ADDRESS_STATE>Pennsylvania</ADDRESS_STATE>
    <ADDRESS_ZIP>19092</ADDRESS_ZIP>
    <LENGTH_OF_SERVICE>10.35</LENGTH_OF_SERVICE>
  </ROW>
  <ROW>
    <EMPLOYEE_FULL_NAME>Cochran, Mr. Bob</EMPLOYEE_FULL_NAME>
    <ASSIGNMENT_NUMBER>396</ASSIGNMENT_NUMBER>
    <EMPLOYEE_NUMBER>396</EMPLOYEE_NUMBER>
    <EMPLOYMENT_CATEGORY>Fulltime-Regular</EMPLOYMENT_CATEGORY>
    <DATE_OF_HIRE>05-FEB-00</DATE_OF_HIRE>
    <GENDER>M</GENDER>
    <ASSIGNMENT_STATUS>Active Assignment</ASSIGNMENT_STATUS>
    <DATE_OF_BIRTH>12-DEC-64</DATE_OF_BIRTH>
    <JOB_NAME>STA700.Staff Administrator</JOB_NAME>
    <MARITAL_STATUS></MARITAL_STATUS>
    <SUPERVISOR_FULL_NAME>Gray, Mr. Donald R</SUPERVISOR_FULL_NAME>
    <PAYROLL_NAME>Vision Semi'monthly</PAYROLL_NAME>
    <GRADE_NAME></GRADE_NAME>
    <ETHNIC_ORIGIN>White</ETHNIC_ORIGIN>
    <SOCIAL_SECURITY_NUMBER>553-34-9238</SOCIAL_SECURITY_NUMBER>
    <LOCATION_NAME>VS- Washington DC</LOCATION_NAME>
    <GOVERNMENT_REPORTING_ENTITY>Vision Services</GOVERNMENT_REPORTING_ENTITY>
    <ORGANIZATION_NAME>Vision Administration</ORGANIZATION_NAME>
    <PAY_BASIS>MONTHLY</PAY_BASIS>
    <PERSON_TYPE>Employee</PERSON_TYPE>
    <LATEST_HIRE_DATE>05-FEB-00</LATEST_HIRE_DATE>
    <ADDRESS_LINE1>77431 Coolie Court</ADDRESS_LINE1>
    <ADDRESS_CITY>Paramus</ADDRESS_CITY>
    <ADDRESS_STATE>New Jersey</ADDRESS_STATE>
    <ADDRESS_ZIP>07652</ADDRESS_ZIP>
    <LENGTH_OF_SERVICE>9.25</LENGTH_OF_SERVICE>
  </ROW>
</ROWSET>
 
 
CSV:
 
Employee Full Name,Assignment Number,Employee Number,Employment Category,Date Of Hire,Gender,Assignment Status,Date Of Birth,Job Name,Position Name,Marital Status,Supervisor Full Name,Payroll Name,Grade Name,Ethnic Origin,Social Security Number,Location Name,Government Reporting Entity,Organization Name,Pay Basis,Person Type,Adjusted Service Date,Latest Hire Date,Address Line1,Address City,Address State,Address Zip,Length Of Service,Notified Termination Date
"Lamprecht, Mr. Mike",201,201,Fulltime-Regular,01-JAN-98,M,Active Assignment,09-MAY-65,VP300.Vice President,,Married,"Andretti, Mr. Evan",Vision HR Semi-Monthly,100.1.South,White,367-98-3283,VA- Philadelphia,Vision Services,Vision Services,MONTHLY,Employee,,01-JAN-98,983 Mainstream Ave,Atlanta,Georgia,30010,11.35,
"Thomas, Cindy",226,226,Fulltime-Regular,01-JAN-99,F,Active Assignment,10-AUG-68,,,Married,,,,White,222-11-3333,VA- Philadelphia,Vision Services,Vision Services,,Employee,,01-JAN-99,4 Saw Mill Ct.,Philadelphia,Pennsylvania,19038,10.35,
"Che:n, Tony",227,227,Fulltime-Regular,01-JAN-99,M,Active Assignment,28-SEP-63,,,Married,"Thomas, Cindy",,,White,149-21-9421,VA- Philadelphia,Vision Services,Vision Services,,Employee,,01-JAN-99,14 Broad St.,Philadelphia,Pennsylvania,19092,10.35,
"Noe, Renee",228,228,Fulltime-Regular,01-JAN-99,F,Active Assignment,12-FEB-68,MGR500.Manager,510.PROJECT MANAGER,Married,"Ang, Ms. Lay Kuan",Vision Monthly,,Black or African American,214-19-4219,VS- Washington DC,Vision Operations,Vision Administration,MONTHLY,Employee,,01-JAN-99,17 Chestnut St.,Philadelphia,Pennsylvania,19092,10.35,
"Cochran, Mr. Bob",396,396,Fulltime-Regular,05-FEB-00,M,Active Assignment,12-DEC-64,STA700.Staff Administrator,710.ADMIN ASSISTANT,,"Gray, Mr. Donald R",Vision Semi'monthly,,White,553-34-9238,VS- Washington DC,Vision Services,Vision Administration,MONTHLY,Employee,,05-FEB-00,77431 Coolie Court,Paramus,New Jersey,07652,9.25,
 
 
Java Code:
 
import com.ricebridge.xmlman.XmlManager;
import com.ricebridge.xmlman.RecordSpec;
import com.ricebridge.xmlman.XmlSpec;
 
import com.ricebridge.csvman.CsvManager;
import com.ricebridge.csvman.CsvSpec;
 
import java.util.*;
import java.io.*;
 
 
/** Convert a CSV file into an XML file.
 *    In this example, the entire CSV file is loaded into memory. 
 *  This is the simplest case, and it will work so long as you have enough 
 *  memory to hold all the data. If you are dealing with multi-gigabyte files, 
 *  it is better to use the streaming solution: StreamingCsv2Xml.java
 *    For this example, the source file is report.csv. This file was exported
 *  from the Google Adwords report system, so it is the actual data format used
 *  by Google (the values have been changed, however). We have deliberately
 *  used an external CSV file to demonstrate the flexibility of the CSV and
 *  XML Manager components.
 */
public class Csv2Xml {
 
  // public methods
 
  public static void main( String[] args ) {
    File csv = new File("Employee Report10595v1.csv");
    File xml = new File("first.xml");
 
    convert( csv, xml );
  }
 
 
  public static void convert( File pCsvFile, File pXmlFile ) {
 
    // Create the CsvManager that we will use.
    // Actual data only starts on the 8th line.
    CsvManager csvman  = new CsvManager();
    CsvSpec    csvspec = csvman.getCsvSpec();
    csvspec.setEncoding("UTF-8");
    csvspec.setStartLine(2);
 
    // Also create the XmlManager, and
    // set the standard XML header (this is optional).
    XmlManager xmlman  = new XmlManager();
    XmlSpec xmlspec = xmlman.getXmlSpec();
    xmlspec.setHeader( "<?xml version='1.0' encoding='UTF-8'?>\n" );
 
    // Specify the format of the output XML.
    // Notice that the first three fields are absolute. The data records
    // will be grouped by these fields, as parent XML elements.
    String[] fieldpaths 
      = new String[] { 
        "/ROWSET/ROW/EMPLOYEE_FULL_NAME",
	      "ASSIGNMENT_NUMBER",
	      "EMPLOYEE_NUMBER",
	      "EMPLOYMENT_CATEGORY",
	      "DATE_OF_HIRE",
	      "GENDER",
	      "ASSIGNMENT_STATUS",
	      "DATE_OF_BIRTH",
	      "JOB_NAME","",
	      "MARITAL_STATUS",
	      "SUPERVISOR_FULL_NAME",
	      "PAYROLL_NAME",
	      "GRADE_NAME",
	      "ETHNIC_ORIGIN",
	      "SOCIAL_SECURITY_NUMBER",
	      "LOCATION_NAME",
	      "GOVERNMENT_REPORTING_ENTITY",
	      "ORGANIZATION_NAME",
	      "PAY_BASIS",
	      "PERSON_TYPE","",
	      "LATEST_HIRE_DATE",
	      "ADDRESS_LINE1",
	      "ADDRESS_CITY",
	      "ADDRESS_STATE",
	      "ADDRESS_ZIP",
	      "LENGTH_OF_SERVICE",
      };
    
    // The RecordSpec specifies the main record element: keyword
    RecordSpec keyspec
      = new RecordSpec("/ROWSET/ROW", fieldpaths );
    
    // Load all the CSV data.
    List csvdata = csvman.load( pCsvFile );
 
    // Last line is totals, so remove it.
    //csvdata.remove( csvdata.size()-1 );
 
    // Save the data records as XML.
    xmlman.save( pXmlFile, keyspec, csvdata );
  }
 
}

Open in new window

0
Comment
Question by:ramadevimandala
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24335628
Why are you looking for a different api?
0
 

Author Comment

by:ramadevimandala
ID: 24335649
The current api is not free one , i am using the trial version.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 24335696
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ramadevimandala
ID: 24336331
This really works nice, but it uses DOM will it be good if there is large amount of data in CSV file,
I mean how will be the performance?
and one more thing is in CSV file if I have the blank values for columns the XML is not proper it takes the next column value.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24336566
You could turn it into text printing if you're worried about the performance. The other issue sounds like you need to do a bugfix
0
 

Author Comment

by:ramadevimandala
ID: 24336789
Ya I have fixed other issues, also I will test for large data.Thank you for your support.
0
 

Author Closing Comment

by:ramadevimandala
ID: 31579412
Thank you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24336821
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question