aspose file tools*
The moose likes Java in General and the fly likes XSL transformation using Apache Xalan Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "XSL transformation using Apache Xalan " Watch "XSL transformation using Apache Xalan " New topic
Author

XSL transformation using Apache Xalan

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Please be sure to post XML questions in the XML forum. This post has been moved there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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

Java Resources at www.wbrogden.com
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

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
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

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

Joined: May 25, 2008
Posts: 116
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

Joined: May 25, 2008
Posts: 116
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
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

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

Joined: May 25, 2008
Posts: 116
hey thanks for the reply. i understand the problem now


ta.
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
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
Bartender

Joined: Oct 14, 2005
Posts: 18152
    
    8

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.
 
 
subject: XSL transformation using Apache Xalan
 
Similar Threads
JSP error page
How to display jasper report in jsp
BufferedImage to byte[]
can we store an integer in a byte array
PdfBox, do you have to save the .pdf to a file?