View Full Version : Read xml File from local and show in the textarea, How ?

7 Mar 2011, 7:47 AM

How to read a file (xml ) and show in the textarea in extjs,

1.Is it possible to do the functionality in client side itself ?
2. I have tried with java by uplaoding the xml file and read it and send the response as xml string, but i am unable to receive the data in front end (getting exception "return eval("("+json+")")" in extjs.all as )?

My code in extjs

var win = new Ext.Window({
layout: 'fit',
title: 'XML Upload Window',
id: 'winFileRead',
resizable: false,
modal: true,
closeAction: 'close',
closable: true,
plain: true,
items: [{
xtype: 'form',
id: 'frmFileRead',
fileUpload: true,
width: 500,
frame: true,
monitorValid: true,
autoHeight: true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 50,
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
items: [{
xtype: 'fileuploadfield',
id: 'verFileReadCmp',
emptyText: 'Select a File to import',
fieldLabel: 'File',
name: 'file',
buttonCfg: {
text: '',
iconCls: 'upload-icon'
buttons: [{
formBind: true,
text: 'Upload',
handler: function() {
var fp = Ext.getCmp('frmFileRead');
if (fp.getForm().isValid()) {
url: 'viewXml.do',
params: {},
method: 'POST',
waitMsg: 'Uploading your file...',
success: function(fp, o) {
if (Ext.decode(o.response.responseText).success) {
//set the xml value to the textarea Ext.getCmp('textareaXML').setValue(Ext.decode(o.response.responseText).message);
} else {
Util.showAlert('Err', Ext.decode(o.response.responseText).message);
failure: function(response, options) {

Util.showAlert('Err', response.responseText);

exception: function(a, b, c, d) {}
}, {
text: 'Reset',
handler: function() {
var fp = Ext.getCmp('frmFileRead');

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException
UploadFileTO uploadFileTO = (UploadFileTO) command;
List<XmlTO> al=new ArrayList<XmlTO>();
Map<String,String> model = new HashMap<String,String>();

MultipartFile file = uploadFileTO.getFile();
InputStream inputStream = null;
String xmlString="";
String xml=null;
if (file.getSize() > 0) {
inputStream = file.getInputStream();

int readBytes = 0;
byte[] buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0 , 10000))!=-1)
xml=new String(buffer, 0, readBytes);
if (!xmlString.equals("")){
System.out.println( xmlString );
model.put("result", "{success:true, message: \""+xmlString+"\"}");
model.put("result", "{success:false, message: \"File upload error.\"}");
return new ModelAndView("index", model);


Exception message in json eval

<TABLE width=400>
<P style="FONT: 13pt/15pt verdana">The XML page cannot be displayed
<P style="FONT: 8pt/11pt verdana">Cannot view XML input using style sheet. Please correct the error and then click the <A href="javascript:location.reload()" target=_self>Refresh</A> button, or try again later.

<P style="FONT: bold 8pt/11pt verdana">Invalid at the top level of the document. Error processing resource 'http://XXXXXXXX/viewXml.do'. </P><PRE style="LINE-HEIGHT: 12pt; FONT-VARIANT: normal; FONT-STYLE: normal; FONT-SIZE: 10pt; FONT-WEIGHT: normal"><FONT color=blue></FONT></PRE></P>


sorry if code is not formatted .

3.if it is not a right method please prefer or provide the best method which supports major browsers(I.E,FF,Chrome) ?

Thanks for your Time


7 Mar 2011, 8:58 AM
please edit your post and use code tags

7 Mar 2011, 11:16 PM
thanks for teaching code tag.