This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Character conversion error: 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 "Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration m" Watch "Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration m" New topic

Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration m

Rajvinder Singh

Joined: Oct 13, 2004
Posts: 2
Hi guys.

iam facing one problem, and cant work it out why exactly it happening.
Iam putting an inputstream request in to Map and than using SAXBuilder parser to parse the same. but when iam using build() method of SAXBuilde its saying me Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration missing? i dond know why???

here is my code.

InputStream ins = (InputStream)request.getInputStream();
OutputStream fileOut = null;
// Get the lenth of the bytes.
int insLength = request.getContentLength();
System.out.println("inslength ="+ insLength);
String APP_ID = null;
String SERVICE_ID = null;
String ACTION_ID = null;
String DAO_ID = null;

// Set the byte type array length to total length of bytes receive.
byte[] fileDataByte = new byte[insLength];
byte[] fileNamebyte = new byte[insLength];,0,300);
int len = 0;
char ch;

String fileName;
StringBuffer sbFileName = new StringBuffer();
StringBuffer filePath = new StringBuffer();
// Raed the fist 30 bytes to get the file name.
while(len <=300 )
ch=(char)fileNamebyte[len] ;
System.out.print("File Name is ="+sbFileName.toString());
fileName = sbFileName.toString().trim();

System.out.println("Before Hashmap"+ins);
HashMap map = new HashMap();
System.out.println("After Hashmap");

String x = (String)map.get("TESTCODE");

/*String xsource = (String)map.get("XMLSOURCE");
System.out.println("XMLSOURCE"+xsource); */
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder();
// Create the document
Document doc = null;
System.out.println("before DOC");
System.out.println("check 4 test code");
doc ="XMLSOURCE"));
System.out.println("after DOC");
//doc ="XMLSOURCE"));

Element RootElement = doc.getRootElement();
System.out.println("after doc"+RootElement.getText().equalsIgnoreCase("REQ"));
// Scans the Request
if ( (RootElement.getName().equals("REQ"))){
System.out.println("after Req");
Element app = RootElement.getChild("APP");
APP_ID = app.getAttributeValue("ID").trim();
System.out.println("APP_ID ="+ APP_ID);
// Get the SERVICE TAG
Element service = app.getChild("SRV");
// Get the Service ID
SERVICE_ID = service.getAttributeValue("ID").trim();
System.out.println("SERVICE_ID ="+ SERVICE_ID);
//Get the ACTION Tag
Element action = service.getChild("ACT");
//Get the action Action ID
ACTION_ID = action.getText().trim();
System.out.println("ACTION_ID ="+ ACTION_ID);
//Get the DAO Tag
Element dao = service.getChild("DAO");
//Get the action Action ID
DAO_ID = dao.getText().trim();

// Set the path to uplaod the file.
System.out.println("before file path");
filePath = filePath.append("/var/opt/SUNWappserver7/domains/domain1/server1/applications/j2ee-modules/KotakAssist_1/external/" +APP_ID);
System.out.println("File Path ="+filePath.toString());

// write the stream to the file.
fileOut = new BufferedOutputStream(new FileOutputStream(filePath.toString()));
// Now will read the 31st byte to end of line;
System.out.println("file uplaoded sucessfully"+fileName);
and my inputstream is an xml file + xsl file
my xml file format is

<?xml version="1.0" encoding="utf-8"?>
<IN KEY="UID">1</IN>

Tahnks in advance.
Rajvinder Singh
It is sorta covered in the JavaRanch Style Guide.
subject: Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration m
Similar Threads
To know about File Upload
Problem with XML
Sending a XML file as a Servlet response
send files over the socket
Read data to the xml file from the database?