• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert text file into XML

 
Vasant Chandra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems relatively easy. What do have so far? Where are you stuck making progress?
 
Vasant Chandra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic