• 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

XSL transformation using Apache Xalan

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i keep getting an error while trying to transform xml which contains '&' in iplanet 6 which uses Apache Xalan. Any ideas why this is ?? what do i need to do to get around this ??

my code :

ByteArrayOutputStream baos = new ByteArrayOutputStream();
java.io.StringReader xmlReader = new java.io.StringReader(xml.toString());

transformer.transform(new javax.xml.transform.stream.StreamSource(xmlReader),
new javax.xml.transform.stream.StreamResult(baos));

the code fails at this point since 'baos' is returned as null.


thanks in advance.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to post XML questions in the XML forum. This post has been moved there for you.
 
Marshal
Posts: 28177
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
If you have malformed XML, then yes, the parser is going to throw an exception. The way to get around that is to send the XML document back to whoever created it and inform them of the problem. It's their responsibility to produce well-formed XML.

But perhaps you could post more details about the problem? Such as the stack trace from the exception? It's possible that your diagnosis of the problem isn't right.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say you are getting an error but you dont include the exact error message. Java error messages are frequently very helpful.

If you are getting an SAXParseException, you can extract the line number and column number where the fault was noticed.

Bill
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry folks, forgot to include the error msg. here it is :


[19/Feb/2009 09:39:10:6] error: Exception: SERVLET-run_failed: Failed in running template: [App = pubs, Servlet = binaryFileServlet], java.lang.NullPointerException
Exception Stack Trace:
java.lang.NullPointerException
at nz.co.telecom.pubs.actions.BinaryStreamServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at com.netscape.server.servlet.servletrunner.ServletInfo.service(Compiled Code)
at com.netscape.server.servlet.platformhttp.PlatformHttpServletResponse.callUri(Compiled Code)
at com.netscape.server.servlet.platformhttp.PlatformHttpServletResponse.callUriRestrictOutput(Compiled Code)
at com.netscape.server.servlet.platformhttp.PlatformRequestDispatcher.forward(Compiled Code)
at nz.co.telecom.pubs.servlet.ActionHandler.doRequest(Compiled Code)
at nz.co.telecom.pubs.servlet.ActionHandler.doPost(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at javax.servlet.http.HttpServlet.service(Compiled Code)
at com.netscape.server.servlet.servletrunner.ServletInfo.service(Compiled Code)
at com.netscape.server.servlet.servletrunner.ServletRunner.execute(Compiled Code)
at com.kivasoft.applogic.AppLogic.execute(Compiled Code)
at com.kivasoft.applogic.AppLogic.execute(Compiled Code)
at com.kivasoft.thread.ThreadBasic.run(Native Method)
at com.kivasoft.thread.ThreadBasic.run(Native Method)
at com.kivasoft.thread.ThreadBasic.run(Native Method)
at com.kivasoft.thread.ThreadBasic.run(Native Method)
at com.kivasoft.thread.ThreadBasic.run(Compiled Code)
at java.lang.Thread.run(Compiled Code)


and the null pointer is thrown due to the transformation returning null baos object.



ta.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The sample code you included doesn't set baos to null, but something must be. Try writing final before the type, or checking that you don't have duplicate variables named baos. For example:
 
Paul Clapham
Marshal
Posts: 28177
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
Why do you conclude that baos is null? The stack trace says that something in the service() method is throwing a NullPointerException, but if baos were null that wouldn't cause anything in the code you posted to throw that exception. It might be called lower in the stack, in the transform() method, but that isn't what the stack trace says.

Unfortunately you don't have debugging information in your running code (not a really good decision for code which might have bugs), but you have to look for code which calls a method on some variable which might be null. My guess would be the "transformer" variable, but that's just a guess.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok have put some debugs in place and it works out that after calling the transformation i.e

transformer.transform(new javax.xml.transform.stream.StreamSource(xmlReader),
new javax.xml.transform.stream.StreamResult(baos));

if the 'xmlReader' has an '&' in it thats when the error occurs.

It works fine if the 'xmlReader' has '&'


i dont understand how this works. how does the transformer does its transformation ???


ta.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for some reason the post ignored one of the characters, the above is meant to read :

the transformer works if the xmlreader has '&' + 'amp;'
 
Paul Clapham
Marshal
Posts: 28177
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
If you're saying that it fails when the XML document is not well-formed because there's a text node containing an unescaped & character, that isn't surprising. And you're saying that it doesn't fail if you make the document well-formed by escaping the & character correctly as & -- right? Well, yeah. The parser will throw an exception when it finds the part of the document which isn't well-formed.

And by the way, this forum (and HTML) uses the same rule for escaping ampersand characters that XML does. It's evident that you don't understand that rule from your last couple of posts, otherwise you wouldn't have had to make them. The rule is:

If you want an "&" character in an XML text node then you have to escape it as "&". And if you want the forum to display "&" then you should realize that it unescapes those things, so you have to type "&".
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks for the reply. i understand the problem now


ta.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a thought, i am using SAX parser to read the xml content, is it possible that SAX parser is not able to understand the escape character for '&' ??
 
Paul Clapham
Marshal
Posts: 28177
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
It's simple. Parsers understand well-formed documents and throw exceptions on others. For a document to be well-formed, among numerous other things, ampersands in text nodes must be properly escaped.

The parsers have been around a long time and are working correctly. You, on the other hand, haven't been around a long time and don't know what's correct. So if you're looking for a loophole which says there's a parser bug in your way, you aren't going to find one. I encourage you to search out the escaping rules for XML documents.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic