aspose file tools*
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 Spring in Action this week in the Spring 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
Author

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

Rajvinder Singh
Greenhorn

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.
<%
response.setContentType("text/xml");


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;

try{
// Set the byte type array length to total length of bytes receive.
byte[] fileDataByte = new byte[insLength];
byte[] fileNamebyte = new byte[insLength];
ins.read(fileNamebyte,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] ;
len++;
sbFileName.append(ch);
}
System.out.print("File Name is ="+sbFileName.toString());
fileName = sbFileName.toString().trim();

System.out.println("Before Hashmap"+ins);
HashMap map = new HashMap();
map.put("REQUESTTYPE","XML");
map.put("TESTCODE","TRUE");
map.put("XMLSOURCE",ins);
System.out.println("After Hashmap");

String x = (String)map.get("TESTCODE");
System.out.println("TESTCODE"+x);

/*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");
if(map.containsKey("TESTCODE")){
System.out.println("check 4 test code");
doc = builder.build((InputStream)map.get("XMLSOURCE"));
}
System.out.println("after DOC");
//doc = builder.build((String)map.get("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");
//Get the APPLICATION TAG
Element app = RootElement.getChild("APP");
//Get the APPLICATION ID
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 ins.read(fileDataByte) will read the 31st byte to end of line
ins.read(fileDataByte);
fileOut.write(fileDataByte);
fileOut.flush();
fileOut.close();
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"?>
<REQ>
<APP ID="KA">
<SRV ID="KA">
<ACT>GET</ACT>
<DAO>SQL</DAO>
</SRV>
<DATA>
<IN KEY="GET_ACC">
<IN KEY="ROW1">
<IN KEY="ACCOPENID">1</IN>
<IN KEY="UID">1</IN>
</IN>
</IN>
</DATA>
</APP>
</REQ>

Tahnks in advance.
Rajvinder Singh
 
jQuery in Action, 2nd edition
 
subject: Character conversion error: "Malformed UTF-8 char -- is an XML encoding declaration m