aspose file tools*
The moose likes Java in General and the fly likes CSV file structure Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "CSV file structure Validation" Watch "CSV file structure Validation" New topic
Author

CSV file structure Validation

Yashwant Palkar
Greenhorn

Joined: Dec 01, 2010
Posts: 20

I want to validate CSV file structure.
CSV file will have different data-types and different data-types will have different fields.
we want to check number of data-types records against value we have in different count file and predefined fields in each data-type records.
fields will be separated by "|".
Simple Example of CSV file is like

if I have 3 different Data types Acccount-10,Person-20,Order-30
Account have 2 record count,
Person will have 3 records count
and Order will have 5 record count

Account will have field value like Account Number,AmountinAccount,AccountComments
Person will have filed like PersonID,PersonName,PersonAdress
Order will have field like Orderid,OrderName


The CSV file like

10|12|2500|Active
10|12|2500|Active
20|1|Mark|US
20|2|Adren|UK
20|3|David|IN
30|Ship
30|Car
30|Pizza
30|Bricks
30|containers


this will be example of CSV file structure
where 10,20 and 30 are number for Data type.

please suggest me away to validate the structure of this type of CSV files.



Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Maybe try to read the file using one of the CSV libraries listed in the http://www.coderanch.com/how-to/java/AccessingFileFormats page and check if they throw an exception. Be sure to choose one that can handle vertical bars as separators.
Yashwant Palkar
Greenhorn

Joined: Dec 01, 2010
Posts: 20
how can i validate this particular CSV against this format which I have mentioned?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Yashwant Palkar wrote:how can i validate this particular CSV against this format which I have mentioned?


With the lack of detailed syntax for the format I don't think you can validate since the third field is ambiguous for these records


As a starting point you should do as has already been suggested and use a CSV parser to extract the basic fields. Then when you have a syntax for each field check each field against the syntax.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Yashwant Palkar
Greenhorn

Joined: Dec 01, 2010
Posts: 20
Let me state my requirement once again….

We have Counter File which contains following details for each record types.
Struture : RecordType|RecordCount|FieldCount
Example
1|27|9 –Means 27 records of record type 1, and each record has 9 fields
2|34|5 –Means 34 records of record type 2, and each record has 5 fields

We have Data File which contain actual records ordered by record type. Frist field of reach record contains value of Record Type.

Example
1|field1| field2| field3| field4| field5| field6| field7| field8| field9|
1|field1| field2| field3| field4| field5| field6| field7| field8| field9|
1|field1| field2| field3| field4| field5| field6| field7| field8| field9|
.
.
.
Like total 27 records
2|field1| field2| field3| field4| field5|
2|field1| field2| field3| field4| field5|
2|field1| field2| field3| field4| field5|
.
.
.
Like total 34 records

We want to validate Data File against details present in Counter File. More spesifically we want to vaklidate following two things
(1) Number of records per Record Type
(2) Number of fields for each Record Type
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Yashwant Palkar wrote:Let me state my requirement once again….

We have Counter File which contains following details for each record types.
Struture : RecordType|RecordCount|FieldCount
Example
1|27|9 –Means 27 records of record type 1, and each record has 9 fields
2|34|5 –Means 34 records of record type 2, and each record has 5 fields

We have Data File which contain actual records ordered by record type. Frist field of reach record contains value of Record Type.

Examplehad
1|field1| field2| field3| field4| field5| field6| field7| field8| field9|
1|field1| field2| field3| field4| field5| field6| field7| field8| field9|
1|field1| field2| field3| field4| field5| field6| field7| field8| field9|
.
.
.
Like total 27 records
2|field1| field2| field3| field4| field5|
2|field1| field2| field3| field4| field5|
2|field1| field2| field3| field4| field5|
.
.
.
Like total 34 records

We want to validate Data File against details present in Counter File. More spesifically we want to vaklidate following two things
(1) Number of records per Record Type
(2) Number of fields for each Record Type


Once more we are presented with creeping requirements. So you validate the CSV file against a file defining what should be in the file. Then, as has been said twice before, using one of the CSV libraries to read the CSV file. You can then read the definition file to see what should have been in your CSV file and make sure it matches.

I'm a little lost as to why you are having trouble with this.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
OK - how about turn the CSV into an XML document using something like ServingXML - open source toolkit and then validating values with a Schema followed by checking your other requirements in XML?

Bill
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
. . . and welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CSV file structure Validation