aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Convert text file into XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Convert text file into XML" Watch "Convert text file into XML" New topic
Author

Convert text file into XML

Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
Hello All,
I am having a very urgent requirement to convert below fixed length text file into XML file. Please help me in achieveing the same.
Few points: Every line consist of 16 characters
First 3 caharacter (like D02,D03 etc will not be included in the message, coming from the sender just for reference))
Every new record will start from "H" character.
059 (occurrence 1), 040 (occurrence 1) and 084 (more than 1 occ) are the key fields to identify new segment (highlighted below)
The fields within the segments are fixed length (shown below).


File structure:

H01A12B1059C1DE1
D02F12040G123H12
D033084002L123M1
D04234NO12P12Q12
D05R4RR2V1234567


<Record>
<Header>....................1..unbounded
<Key>H</Key>
<Filed1>A12</Field2>
<Field2>B1</field2>
<One>
<Key>059</Key>
<Field3>C1</Field3>
<Field4>D</Field4>
<Field5>E1</Field5>
<Field6>F12</Field6>
</One>
<Two>
<Key>040</key>
<Field7>G123</Field7>
<Field8>H123</Field8>
</Two>
<Three>
<Key>084</Key>
<Occ>002</occ>
<Repeat>............................1..unbounded
<Field9>L123</Field9>
<Field10>M1234</Field10>
<Field11>NO12P12</Field11>
</Repeat>
<Repeat>
<Field9>Q12R</Field9>
<Field10>4RR2V</Field10>
<Field11>1234567</Field11>
</Repeat>
</Three>
</Header>
</Record>


Regards!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
Seems relatively easy. What do have so far? Where are you stuck making progress?


Ping & DNS - my free Android networking tools app
Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
Ulf Dittmer wrote:Seems relatively easy. What do have so far? Where are you stuck making progress?


Hi,
Thanks a lot for the reply!!
I am not from a java domain so not sure how i will convert this fixed file into xml as shown above.
Do you have any pointers, or if possible please share the raw peice of code i will try to manipulate it.
Thanks In Advance.

Regards!!
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

To create XML files as well as read them you should use Document Object Model. For example:
Method getNestedElement:

Above code parses this xml file:


True person is moral, false is right!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
If you haven't worked with XML in Java before, then the easiest may be to use the classes in the java.io package to read the flat file and then write out the properly formatted XML file.
Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
Volodymyr Levytskyi wrote:
To create XML files as well as read them you should use Document Object Model. For example:


Hello,
Thanks for the reply!!
It looks that you are parsing/reading the XML file and converting it into text but i want just the opposite of this. I want to read the text file(shown above) and then convert it into XML format. Can you please share the code for the same.

Thanks!!
Vasant Chandra
Greenhorn

Joined: Oct 06, 2012
Posts: 10
Hi,

Ulf Dittmer wrote:If you haven't worked with XML in Java before, then the easiest may be to use the classes in the java.io package to read the flat file and then write out the properly formatted XML file.


Do you have any sample code for the same which reads scattered fixed length data and convert into XML.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
No. If you haven't used java.io before, now may be a good time to learn about it. The Oracle Java Tutorial will get you started, as will any introductory book.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Convert text file into XML