aspose file tools*
The moose likes XML and Related Technologies and the fly likes Reading and writing fixed length file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Reading and writing fixed length file" Watch "Reading and writing fixed length file" New topic
Author

Reading and writing fixed length file

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
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
Marshal

Joined: Mar 22, 2005
Posts: 41819
    
  62
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?


Ping & DNS - my free Android networking tools app
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
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
Marshal

Joined: Mar 22, 2005
Posts: 41819
    
  62
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

Joined: Nov 03, 2003
Posts: 116
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

Joined: Oct 13, 2005
Posts: 38788
    
  23
Aah, it's about XML. I thought this thread fitted badly here. Moving.
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Nov 03, 2003
Posts: 116
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

Joined: Nov 03, 2003
Posts: 116
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

Joined: Nov 03, 2003
Posts: 116
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Ryan Jimmy,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading and writing fixed length file