PDA

View Full Version : Reading XML file in local system



narayantalakanti
24 Aug 2007, 9:54 AM
I have an XML file in my local machine. If I use the following code only the headers are displaying. Is there a way that I can display the XML data without using HttpProxy? Code looks like below. Please help!

var ds = new Ext.data.Store({
// load using HTTP
proxy: new Ext.data.HttpProxy({url: 'xml/claims-diag-chaps.xml'}),

// the return will be XML, so lets set up a reader
reader: new Ext.data.XmlReader({
// records will have an "row" tag
record: 'row'
//id: 'ASIN',
//totalRecords: '@total'
}, [
// set up the fields mapping into the xml doc
'DiagnosticChapter','_2006Medical','_2006RxDrug',' _2006TotalPaid','_2005Medical','_2005RxDrug','_200 5TotalPaid'
])
});

If I post the files in the webserver everything works fine but I want to run it locally...

hendricd
24 Aug 2007, 10:08 AM
See if this (http://extjs.com/forum/showthread.php?t=10672) might help you.

narayantalakanti
24 Aug 2007, 10:22 AM
Thanks for quick reply. I cannot able figure out from this. Is there a small example folder which you can share with me like with one html file which is linked to js and xml file?

hendricd
24 Aug 2007, 10:28 AM
No, just include the localXHR.js script somewhere below your ext-base.js script tag in your example and try it again. Like this:



script type="text/javascript" src="src/ext-base.js"></script>
<script type="text/javascript" src="src/ext-all.js"></script>
<script type="text/javascript" src="src/localXHR.js"></script>

narayantalakanti
24 Aug 2007, 10:35 AM
Hey perfect it's working! So helpful... Thank you so much...

ceeeed
9 Sep 2007, 10:21 PM
I am having a similar problem of getting data from a local file. When I use a local variable for the data, I am able to get the count correctly, but when I try to read from a local file where the exact same data is stored, I get the count written out as 0.

I am including the localXHR code mentioned in this thread earlier.

I am including the code below. I have also checked with Firebug and it does not seem to complain about anything.

I have also noticed that in Firefox (on mac os), the page seems to be "loading" even after the count of items have been written out.

PS. I have just started learning about Ext, so it is quite likely I may be doing something strange.

Thanks in advance for your help.


Here is the html code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>JSON Example</title>

<!-- CSS includes -->
<link rel="stylesheet" type="text/css"
href="../ext-1.1.1/resources/css/ext-all.css" />

<!-- Include ext javascript code -->
<script type="text/javascript"
src="../ext-1.1.1/adapter/ext/ext-base.js"></script>
<script type="text/javascript"
src="../ext-1.1.1/ext-all.js"></script>
<script type="text/javascript"
src="../ext-1.1.1/ext-all-debug.js"></script>
<script type="text/javascript" src="../ext-1.1.1/localXHR.js"></script>

<!-- Local css -->

<!-- Local javascript code -->
<script type="text/javascript" src="json_ext.js"></script>
</head>

<body>
<h1>JSON example</h1>

</body>
</html>

and here is the javascript code. I have commented out the HttpProxy part here and left in the MemoryProxy part where I store the data in a local variable.

Ext.onReady(function() {
var reader = new Ext.data.JsonReader({root: 'items', id: 'name'}, ['name', 'type']);
/* does not seem to be working
var proxy = new Ext.data.HttpProxy({url: './simple.json'});
*/
var json_data = {
items: [
{ name:'Africa', type:'continent' },
{ name:'Europe', type:'continent' },
{ name:'Asia', type:'continent' }
]}
var proxy = new Ext.data.MemoryProxy(json_data);
var store = new Ext.data.Store({proxy: proxy, reader: reader});
store.load();
var num_items = store.getCount();
document.writeln("Number of records = " + num_items);
});

hendricd
10 Sep 2007, 5:53 AM
@ceeeed - The underlying Ajax calls are made asynchronously, so your call to getCount() is premature as written. You must setup an onload event (http://extjs.com/deploy/ext/docs/output/Ext.data.Store.html#events) (fired after the ajax and load call completes) to examine results:

And, get into the habit of setting up for errors/debugging too. Define loadexception (http://extjs.com/deploy/ext/docs/output/Ext.data.Store.html#events) events as well:


store.on(
{'load': function(ds,data,args) {
var num_items = this.getCount();
document.writeln("Number of records = " + num_items);
}
, 'loadexception': function(ds,data,args,exception) {
console.log(['dataStore Trouble',arguments]); }
, scope:store}
);
//and again for the Proxy
store.proxy.on(
{'load': function(proxy,data,args) {
console.log(['received something',arguments]);
}
, 'loadexception': function(proxy,data,args,exception) {
console.log(['Proxy Trouble',arguments]); }
,scope:store.proxy}
);
store.load({});

Ajax stuff requires some pessimism. ;)

ceeeed
10 Sep 2007, 6:35 AM
Thanks a lot. That worked and helped me get over a hump. I have two follow-up questions.
Whether I use a local variable or load the data from a local file, the web page continues to "load" even after the number of records have been printed out from the handler. Not sure why/where this is happening. Do i need to write another event handler?
In this case since the file is always guaranteed to be available, is it possible to read this file syncrhronously? What options do I need to set in the proxy and how do the event handlers need to be modified?


Thanks again for your help.

hendricd
10 Sep 2007, 7:13 AM
RE #1, [Theory] Try setting this early in your Ext.onReady:


Ext.BLANK_IMAGE_URL = 'somewherelocal/src/resources/images/default/s.gif';
By default, the Ext framework uses the Extjs.com website to load this image. You need to change it for your configuration.

RE: #2, (Synchronous calls are supported only with localXHR.js) The event handlers (as I presented earlier) won't require any modification, but you'll need to implement a third on the proxy:


store.proxy.on('beforeload',function(proxy,params){
proxy.getConnection().on('beforerequest',function(me,options){
options.async=false;
});
},
store.proxy);

But, SYNCHRONOUS calls are for wussies. ;)

ceeeed
10 Sep 2007, 8:06 AM
Thanks again. I will try out the response to #2. As far as #1 is concerned, I noticed that if I replace the document.writeln() with elem.update() (after leaving a placeholder in the html code), it does not keep "loading"...

hendricd
10 Sep 2007, 8:10 AM
Yes, you don't want to use document.writeln after the document has been rendered.

iWork.F3
17 Dec 2007, 1:29 AM
see if this help you