• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flat file to XML COnversion

 
deep venu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If nebody cld give me pointers as to how to go about converting a flat file(delimited) into a XML file..please let me know. If you have a sample code or something which you cld share it would be gr8. This is couase I may not have the liberty to use a tool..like XML Convert or XMl spy..
 
Rakesh Gudur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I don't have the exact code but you can try this way.
Read the flat file, get the values and it's name (but this would be a hardcoded method) based on the delimiter using the Java StringTokenizer available in the util package. Based on the name, you can construct an XML file. This method doesn't need any XML package or parser.
This is how I guess it will work. Let me know once you get the sol.
Rakesh.
If something can go wrong, it will.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there might be lots of approaches..
1. One you can read the file and create xml tags and output content.
3. you know how the delimited file looks like , so you might want to create a DOM out of it and then serialize the dom. apache's xerces comes with classes to serialize a dom object to a file.
OR
3. You may use JAXP and get the thing done using transformation packages in JAXP.
a. you might create a DOM out of your de-limited file.
specify the Source as the DOM which you just created
Transformer transformer =
transFact.newTransformer();
/* this means an identity stylesheet will be sed which will copy the source to the result*/
Source xmlSource = new DOMSource(domObj)
Result result = new StreamSource(new File("test.xml"));
transformer.transform(xmlSource,result);
3. You might use JAXP with sax as well.
For that you might have to write an implementation of the XMLReader interface, i mean just the parse method is significant.
Source source = new SAXSource(new YourReader(),
new FileInputStream("your-delimited-file");
Result result = new StreamSource(new File("test.xml"));
transformer.transform(xmlSource,result);
In your implementation of the parse() method of XMLReader, you have to act as an "event producer" for the content handler implemented by the processor.
ie you have to make calls to the
startElement() , endElement() and characters() method.
after getting the handle to the ContentHandler.
HTH



Originally posted by deep venu:
If nebody cld give me pointers as to how to go about converting a flat file(delimited) into a XML file..please let me know. If you have a sample code or something which you cld share it would be gr8. This is couase I may not have the liberty to use a tool..like XML Convert or XMl spy..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic