File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Handling special chars to avoid  UTFDataFormatException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Handling special chars to avoid  UTFDataFormatException" Watch "Handling special chars to avoid  UTFDataFormatException" New topic
Author

Handling special chars to avoid UTFDataFormatException

Dhananjay Inamdar
Ranch Hand

Joined: Jan 27, 2003
Posts: 130
Hi,

On my weblogic server 8.1, I have a small piece of code which reads xml data and generates reports in PDF format.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.InputSource;

//HERE the xslTransform is passed as a StringBuffer of size 512
public String transform (String xmlData, String xslTransform) throws Exception
{
String outputXML = null;

//1. first convert the input xml to a source
Source dataSource = createSource(xmlData);

//2. convert this xsl too to a Source
Source xslSource = createSource(xslTransform);

//3. convert the xsl to a Transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xslSource);

//4. create a output result for the output of the transform
StreamResult result = createResult();

//5. transform
try
{
transformer.transform(dataSource,result);
}
catch(Exception e)
{
e.printStackTrace(); // THE ERROR PRINTED HERE is java.lang.OutOfMemoryError
}

//6. get the String the stream result
//the following works because toString is overridden for ByateArrayOutputStream

outputXML = result.getOutputStream().toString();

//7. return the string result
return outputXML;

}

private Source createSource(String xml)
{
//1. create a byte array
byte[] array = xml.getBytes();

//2. create a inputstrem for this array
ByteArrayInputStream xmlStream = new ByteArrayInputStream(array);

//3. now create a SAXSource from this stream
Source xmlSource = new SAXSource(new InputSource(xmlStream));

return xmlSource;
}

private StreamResult createResult()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
return new StreamResult(baos);
}

But if the xml size is too big, I get java.lang.OutOfMemoryError.

Can you please give me a solution for this?

Also, even if my xml is small and it contains some special characters like �Threshold is �3,000�

It gives me the following exception:

javax.xml.transform.TransformerException: java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence.

How to handle these special charaters?


-Dhananjay


Just like you, struggeling to get the right solutions!<br /> <br />Sun Certified Java Programmer 1.5<br /> <br />Target - SCWCD
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
1. Special characters will have to be changed to some valid Unicode

byte[] array = xml.getBytes();
// why not go through that array byte by byte before passing it to the transform

2. Assuming that you are not running out of memory due to some program loop, give your server more memory

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling special chars to avoid UTFDataFormatException
 
Similar Threads
converting XML with XSLT
Question Mark's appearing in MS word document Generated using XML, XSL FO and jFor
Returning a byte-array after performing an XSLT
Is The XSLT Transformation Platform Independent?
transforming xml from string source