aspose file tools*
The moose likes XML and Related Technologies and the fly likes Which Java Technology to Use to Convert a Text File to a XML 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 "Which Java Technology to Use to Convert a Text File to a XML File" Watch "Which Java Technology to Use to Convert a Text File to a XML File" New topic
Author

Which Java Technology to Use to Convert a Text File to a XML File

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I have a text file looks like:


And the file needs to be converted to a XML file looks like:


Which is the proper Java technology to perform such a task? Thank you.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
You would need to use the Java I/O libraries for reading contents of text files and writing contents of text files. And you most likely would need to use Java's Regular Expression libraries for pattern matching and such. Aside, Perl is actually a much stronger language than Java for this type of task.

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I agree. Some customization is needed. Thank you.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Sounds good. At this point, there is nothing to "customize" however. In order to create a data conversion program to create XML documents, you would use the existing Java libraries in conjunction with your own code. Here you are not creating any type of "customization" of existing Java libraries and your portion does not exist so it cannot be "customized."

Once you create the application, then you can think of "customizing" it for additional business requirements that may come in the future.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

In the past I have used the rather obscure technique of taking an identity transformation and feeding it a stream of SAX events. When you use a SAX parser it passes a stream of SAX events to your ContentHandler, which means in practice that it calls your ContentHandler's startDocument and startElement and characters methods (etc) in the right order. My technique is to turn that around. Here's a brief outline extracted from some of my code:



This is just a crude outline, notice that I've hard-coded constants which would normally be passed to the code as method parameters, but it is working code which outputs a simple XML document.

An advantage of this is that it leverages the knowledge of the built-in Java classes so that you don't have to concern yourself with escaping of ampersands and quoting of attributes and all of that tedious stuff.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which Java Technology to Use to Convert a Text File to a XML File