Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading and writing fixed length file

 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Was wondering what would be a good way to come up with a generic solution where I can some how use a xsd file to read a fixed length data file? I would be using the file data to do a database transaction using Hibernate. So, basically I would be using the xsd file to parse the file as well as do initial data validation.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "xsd file", do you mean an XML Schema? If so, how is that related to a fixed-length file? And what would databases (or Hibernate) have to do with the file contents?
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if I could use a xsd file in the context of an xml file parsing after a fixed length file is transformed into a corresponding xml file/object. Mentioning Hibernate does not have anything to do with the file parsing. I guess shouldn't have mention about that to create any confusion. Just a thought, in any way javax.transform.xml or JAXB would be any use for what I want to achieve? Or there are much simpler way in doing this.

Thanks
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not clear on what you're trying to do: There is a fixed-length file (which is not XML) you want to parse. Then you want (need?) to transform that into XML? What is the point of that if you want to get it into a database?

A schema can be used to validate an existing XML file, not to create a new one, so it doesn't sound as if it would be of much help with this.
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. My mistake. What I actually want to use is some form of an xml file where it would have fields and it's character positions. This file would be used to parse a fixed length file by file reader and writer code.

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aah, it's about XML. I thought this thread fitted badly here. Moving.
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like to also mention that the fixed length file reader and writer would create a POJO and write to a fixed length file from a POJO.
 
Paul Clapham
Sheriff
Pie
Posts: 20948
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tariq Ahsan wrote:What I actually want to use is some form of an xml file where it would have fields and it's character positions. This file would be used to parse a fixed length file by file reader and writer code.

So basically the XML file acts as a configuration file which describes the format of the flat file? Okay. Sure, you could write a program which does that. I don't see where an XML schema comes in, though, since it's supposed to describe an XML document and this XML document you have is pretty simple. I suppose you could have one, but I don't see where it helps in writing this program you're proposing.
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I came up with to read a file with fixed positions records and parse it contents by using a XML property file. This meta data XML file contains the lower and upper boundary of each field's character positions.

The readFixedLengthFile method of the FixedLengthFileReader class takes the arguments of the input data file and the xml property file. Each line of the input file is read and by using the xml file's field description it is parsed and stored in a HashMap object. This HashMap object is then returned back to the caller.



Here's client application -



The input data file - Test.txt :



XML property file - Test.xml :



The console output from the client run -



 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had to make some changes to the return type of the readFixedLengthFile method from Map to List object.
Here's the code change -


Had to change the client program too.


 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I seeing little problem when I am reading the properties stored as an Enumeration object. While reading each <entry> is not getting retrieved according to the order it is in property xml file.

Is there anyway, I can retrieve these field value in the same order the elements are in the property file?

Thanks
 
Paul Clapham
Sheriff
Pie
Posts: 20948
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Properties object stores its data in a Map, which by default doesn't order its entries in any particular way. So if you want your
data to be ordered, don't use a Properties object to store it.
 
Paul Clapham
Sheriff
Pie
Posts: 20948
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Jimmy,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic