*
The moose likes XML and Related Technologies and the fly likes Creating XML from fixed length text file using XSL 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 "Creating XML from fixed length text file using XSL" Watch "Creating XML from fixed length text file using XSL" New topic
Author

Creating XML from fixed length text file using XSL

Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Hi,
I would like to convert a fixed length text file to XML using XSL.
I got a sample code but its in ASP.
http://www.devx.com/getHelpOn/10MinuteSolution/20362/0/page/1

I have a similar requirement but i am not able to find equivalent APIs in Java.

Any help is appreciated.
Thanks,
Karan
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
use Xalan


Amit Tank
Linked In
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I think using XSL you can convert an XML to any other format.May be text or any other XML form.

But I do not know of anything that would convert a text chunk to XML.For that any markup generator is sufficient.Like Apache's Velocity.


Rahul Bhattacharjee
LinkedIn - Blog
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
The ServingXML toolkit appears to be exactly what you are looking for. I found it while doing research for this article.

Bill
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Thanks for the reply.

I am actually very new to J2EE and XML.
Just wanted to check if
http://www.topxml.com/xsltStylesheets/xslt_non_XML.asp would help me.
If this will help me, How to invoke this XSL using the input text file to get the XML back? I mean to say which Java api will do it for me.
Will JDOM can help me with this?

Thanks,
Karan
[ July 05, 2007: Message edited by: Karan Jain ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
No need to get involved with JDOM, the standard Java library has all you need. Look at the javax.xml.transform package to start.

Bill
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Hi,
I am not able to write the code to pass text file object (or string) and xsl in an API to give me back a XML.

Any sample code is appreciated.

Thanks...
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
XSLT only works with xml file, it does NOT work with your non-xml-text-file, period!

Think alternatives!

Thanks!
[ July 09, 2007: Message edited by: Roseanne Zhang ]
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Thanks Roseanne for clearing this out.
I was thinking in that direction looking at the an example on
http://www.topxml.com/xsltStylesheets/xslt_non_XML.asp
Please have a look and let me know if they are using some customize api for the same.

Thanks...
[ July 09, 2007: Message edited by: Roseanne Zhang ]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
1) Download the source
2) Take a look at the file FixedLangthTextToXML,xml
3) You should know the trick it is using immediately, and you should know what I said above is true.

here is the trick:


They used an entity to put the non-xml-text into the <data></data> tag to make it XML first. Of course, you can do the same.

To tell you the truth, I don't like such trick...
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
You can use JavaScript/Perl/Ruby or whatever to do the work in a more flexible way instead to coarce xsl to do something it is not supposed to do.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Karan Jain:
Hi,
I am not able to write the code to pass text file object (or string) and xsl in an API to give me back a XML.


As Roseanne Zhang mentioned its not possible.I have also mentioned this in my first post.

Why do not you try Apache's Velocity ?

  • First you have to parse the text file and pick up the values that you require to construct the XML and put those in some java bean instance.
  • Write a velocity template file to reflect the structure of the final XML file.
  • Now use velocity engine to generate the XML with the bean as input to that.

  • Karan Jain
    Ranch Hand

    Joined: May 30, 2007
    Posts: 82
    Thanks Roseanne and Rahul for your valuable inputs. Sorry i didn't realize the trick.

    Thanks and regards,
    Karan
     
    wood burning stoves
     
    subject: Creating XML from fixed length text file using XSL