• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. Some customization is needed. Thank you.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic