Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:27:39+00:00 2026-05-31T17:27:39+00:00

I am using opencsv to parse text file and generate List<String[]> now I want

  • 0

I am using opencsv to parse text file and generate List<String[]> now I want to generate XML from List<String[]> and so my question is do we have any 3rd Party Libraries that does that conversion, if not, what would be an better approach to solve the issue.

Here is the Parsing Logic:

    public class ParseFile {

    public ParseFile() {

    }

    public void getQuotes() {
        String fileName = "C:\\GS.txt";
        try {
            CSVReader reader = new CSVReader(new FileReader(fileName), '\t');
            String[] nextLine;
            List<String[]> dataList = new ArrayList<String[]>();
            dataList = reader.readAll();
        } catch (Exception e) {
        }
    }

    public static void main(String args[]) {
        ParseFile test = new ParseFile();
        test.getQuotes();
        System.out.println("Parsing Done Successfully....");
    }
}

So my txt file looks like:

Header Information: ContractDate Trader  Quantity
1st Line of Data:   03/23/12     GS      100

and I need to have XML Structure like:

<root>
  <entry id='1'>
    <ContractDate>03/23/12</ContractDate>
    <Trader>GS</Trader>
    <Quantity>100</Quantity>
  </entry>
</root>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:27:40+00:00Added an answer on May 31, 2026 at 5:27 pm

    You don’t explicitly require a 3rd party library to generate your rather simple XML format. You could simply write a to a StringBuffer object:

    ...
    StringBuffer sb = new StringBuffer();
    sb.append("<root>");
    for (String[] row: dataList) {
      sb.append("<entry>");
      sb.append("<ContractDate>").append(row[0]).append("</ContractDate>");
      // etc...
      sb.append("</entry>");
    }
    sb.append("</root>");
    ...
    

    However, I’m not necessarily advocating it. I personally recommend Simple XML. Create a POJO to model your rows (I won’t include all properties in my example):

    @Root
    public class Entry {
    
      @Element
      private String trader;
    
      @Attribute
      private int id;
    
      public Entry() {
        super();
      }  
    
      public String getTrader() {
        return text;
      }
    
      public void setTrader(String trader) {
        this.trader = trader;
      }
    
      public int getId() {
        return id;
      }
      public void setId(int id) {
        this.id = id;
      }
    }
    
    @Root(name="root")
    public class Entries {
       @ElementList(inline=true)
       List<Entry> entries;
    
       public Entries(List<Entry> entries) {
         this.entries = entries;
       }
    }
    

    It’s certainly some extra work up-front, but for non-trivial programs, having proper Java classes to represent the data models makes a lot of sense and provides greater flexibility, and code clarity.

    To save save as XML you do this:

     List<Entry> entryList = new ArrayList();
     int entryCount = 0;
     Serializer serializer = new Persister();
     for (String[] row: dataList) {
       Entry entry = new Entry();
       entry.setTrader(row[1]);
       entry.setId(++entryCount);
       entryList.add(entry);
     }
     File result = new File("entries.xml");
     serializer.write(new Entries(entryList), result);
    

    The Simple XML library gives you a lot of flexibility. You can also load up the XML file back into a list of Entry objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using OpenCSV to parse a CSV file and bind its values directly to
I'm using opencsv and want to write to a .csv file through multiple sessions.
I've got a CSV file that I'm processing using the opencsv library. So I
This is simple text detection video made using an opencv. Any ideas how was
I am using scala 2.7.7, and wanted to parse CSV file and store the
I'm using OpenCV 2.3.2 library downloaded from OpenCV trunk. I want to use setOpenGlDrawCallback
I am reading a csv file using opencsv. I am ignoring the first line
I want to do background subtraction in a video file using OpenCV method. Right
Really newbie question: I have a .csv file that I need to read. I've
Using OpenCV, saving a CvMat structure into a YAML file on the disk is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.