Explanation and Fix:
Sometimes using JAXB, you might need to write following output to the OutputStream object of browser. Intent is to show a minimal UI
on browser by using a XSL file and the XML attributes.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type='text/xsl' href='../myviewer.xsl'?>
<FileDate>
<DataName>file1.txt</DataName>
</FileData>
To implement this, in your Java code you will write something like this:
StringWriter sw = new StringWriter();
JAXBContext context = JAXBContext.newInstance(FileDate.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", RESOURCE_XSL);
marshaller.marshal(FileDate, sw);
Console.WriteLine(sw.getBuffer().toString());
response.setContentType("application/xml"); //response is object of HttpServletResponse.
response.getWriter().print(sw.getBuffer().toString());
This works perfectly if you are using JAXB 2.2/ java6 or more. but if your system is confused between old and new java, then it throws this exception:
Console.WriteLine(sw.getBuffer().toString()); -->will throw exception
javax.xml.bind.PropertyException: name: com.sun.xml.internal.bind.xmlHeaders value: <?xml-stylesheet type='text/xsl' href='../myviewer.xsl'?>
This exception is random and does not appear on all systems. To fix this, use try-catch block and try to use this: com.sun.xml.bind.xmlHeaders
so final code to correct this error will be:
try{
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", RESOURCE_XSL);
}
catch(PropertyException pex)
{
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", RESOURCE_XSL);
}
It should fix the issue.
4 comments:
Hurrah! After all I got a blog from where I be able to truly obtain valuable information concerning my study and knowledge.
my web page; acheter followers twitter
My web page: Achat Retweet pas cher
Hi! This is my 1st comment here so I just wanted to give a quick shout out and tell you
I truly enjoy reading through your posts. Can you suggest any other blogs/websites/forums that cover the same
topics? Thanks a lot!
Also visit my page ... acheter des Vues Sur youtube
Also see my web site: vues youtube
This solved an issue I'd been grappling with for days. Thank you!
You have solved a problem that was causing me problems for days, thank you very much :-)
Post a Comment