• Post Reply Bookmark Topic Watch Topic
  • New Topic

Flat File to XML convertion  RSS feed

 
Rajesh Kumar Swain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a flat file and I want to covert it to XML file by reading it and that flat is with |(pipeline as separator) content of the flat file is as follows

NAME|ADDRESS|MOBILE
rajesh|puri|9873930665
padu|cuttack|9910108576


and the required XML Format is


<DETAILS COUNT='1'>
<NAME>rajesh</NAME>
<ADDRESS>puri</ADDRESS>
<MOBILE>9873930665</MOBILE>
</DETAILS>
<DETAILS COUNT='2'>
<NAME>padu</NAME>
<ADDRESS>cuttack</ADDRESS>
<MOBILE>9910108576</MOBILE>
</DETAILS>

Thanks in advance

Rajesh
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which specific problems are you facing implementing this? Are you familiar with the java.io package for reading and writing files? In this case I wouldn't use any of the XML APIs - just regular file I/O.

The lines of the input file can be separated by using the StringTokenizer class.
 
Rajesh Kumar Swain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggetion.
I got the answer.But Now I am facing problem in reading the null value as follows

jitu||9871162097

It's giving 2 tokens. But as per myrequirement it should give me 3 tokens as follows

1.jitu
2.
3.9871162097

can anyone suggest anything
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you're using StringTokenizer? There is an optional argument to make it give you the delimiters as well as the values between them. You can add some logic to detect empty columns by two delimiters in a row.

See also Scanner which can parse with regular expressions. I'm far from expert at expressions, but I bet you could teach it to recognize missing columns.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!