Solved

Simple Algorithm\Logic

Posted on 2003-11-14
10
286 Views
Last Modified: 2010-03-31
The database query would fetch more than 1000 records as resultset.

Now, I have to loop through each record and extract values from records and each record would be considered as a client request. Each client request is formed as a XML and written into a file. Limitation is each file should contain ONLY 200 requests (records). How should I dynamically create the XML objects for every 20 records and put them in a file.

I.e. The 1000 records should result in 5 files with 200 records (XML requests) each. Could somebody help me in doing this?

Thanks.
0
Comment
Question by:innumonenu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9747529
What code do you have at the moment?

Is this homework? ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9747562
Just create a new xml insert every 200 reads:

int numRecords = 0;
StringBuffer xmlInsert = new StringBuffer();
while(resultSet.next()) {
    ++numRecords;
    xmlInsert.append("<root>");
    xmlInsert.append("<node1>");
    xmlInsert.append(rs.getString(1));
    xmlInsert.append("</node1>");
    if (numRecords == 200) {
       xmlInsert.append("</root>");
       doInsert(xmlInsert.toString();
       xmlInsert.setLength(0);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9747566
>>Is this homework?

I hope not!
0
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!

 
LVL 15

Expert Comment

by:JakobA
ID: 9751508
Assuming your database is SQL driven i would use the LIMIT clause in the select statement

     SELECT whatever
     FROM whereve
     WHERE conditions
     ORDER BY sequence
     LIMIT   first, number

before limit is your normal request. producing 1000 records
Limit then is a way of aying: but only send the ones from nr 'first' and 'number' forvard
    Limit 0, 5      // the first five
    Limit 5, 5      // the next 5
...

You wil be doing several queries, but each return only records enough for one XML file

regards JakobA
0
 
LVL 92

Accepted Solution

by:
objects earned 40 total points
ID: 9752925
Storing the entire file contents in a string is unnecessary and could use lots of memory.
Instead simply write the xml directly to the file, and switch files every 200 records.

FileOutputStream out = null;
int n = 0;
while(resultSet.next())
{
    if (n%200==0)
    {
       if (out!=null)
       {
          // Close currently open file
          out.close();
       }
       // Open next file
       out = openNextFile();
    }

    // write xml to out as required
    ....

    n++;
}
0
 

Author Comment

by:innumonenu
ID: 9753881
Thanks all for the response. It is not a home work :-)

The code I currently have is creation of XML and put into a file for records less than 200.
Only logic missing is - if more than 200 records then split them into different files containing only 200 records. Also, restricting in the SQL query is not desirable for us our requirments. We have to fetch all existing records and split them in the java program.

I would try the other options listed on monday and update you all. Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9753963
>>Only logic missing

This should cope with any number:

int numRecords = 0;
StringBuffer xmlInsert = new StringBuffer();
while(resultSet.next()) {
      ++numRecords;
      xmlInsert.append("<root>");
      xmlInsert.append("<node1>");
      xmlInsert.append(rs.getString(1));
      xmlInsert.append("</node1>");
      if (numRecords == 200) {
            writeToFile(closeRoot, xmlInsert);
      }
      if (xmlInsert.getLength() > 0)
            writeToFile(closeRoot, xmlInsert);
      }
}

public void writeToFile(String closeRoot, StringBuffer xmlInsert) {
      xmlInsert.append(closeRoot);
      // Do the file writing, calling xmlInsert.toString for the contents
      xmlInsert.setLength(0);
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 60 total points
ID: 9754098
Oops - just spotted an error:

>>if (numRecords == 200) {

should be

if (numRecords % 200 == 0) {
0
 

Author Comment

by:innumonenu
ID: 9776090
Thanks all for the help
0
 
LVL 92

Expert Comment

by:objects
ID: 9776120
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

627 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