• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Natalie Kopple
Ranch Hand
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 325
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. Some customization is needed. Thank you.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20208
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic