Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating XML from fixed length text file using XSL

 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use Xalan
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Roseanne and Rahul for your valuable inputs. Sorry i didn't realize the trick.

    Thanks and regards,
    Karan
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic