PDA

View Full Version : Reading the xml file



samwells
29 Oct 2008, 8:33 PM
I want to read a xml file from the client which is in server and want to display the whole content on a page or want to perform some operation on the data

I went through the example in extjs site XmlGridExample which reads the xml file and
shows the data in a table. This part of the code i m trying to use


ModelType type = new ModelType();
type.root = "records";
type.recordName = "record";
type.addField("name");
type.addField("age");
type.addField("city");
type.addField("state");

// use a http proxy to get the data
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
"data.xml");
HttpProxy proxy = new HttpProxy(builder);

// need a loader, proxy, and reader
XmlReader reader = new XmlReader(type);

BaseListLoader loader = new BaseListLoader(proxy, reader);

ListStore store = new ListStore<ModelData>(loader);

loader.load();
---------------------------------------------------------------------------
This code works when its linked with gird . But i don't want to show this records in
the gird
I just want to get the data in the liststore . But i m always getting null pointer
exception. because the getcount method always returns 0 . There is no data in the
store
System.out.println("Total Records : " + store.getCount());

How to get the records in the xml file in the store

Thanks in advance
Sam Wells

gslender
29 Oct 2008, 10:59 PM
remember the load happens async - when are you checking it?

samwells
29 Oct 2008, 11:08 PM
I tried both the way like on a button click event as well as sequential like this
BaseListLoader loader = new BaseListLoader(proxy, reader);
ListStore store = new ListStore<ModelData>(loader);
loader.load();
Both places i get store count zero

In the example given in the ext site
http://extjs.com/explorer/#xmlgrid

If I just print the store count in the button click event of load button
after the loader.load() ,still i get the value as zero

Sam Wells

gslender
30 Oct 2008, 5:20 AM
show me the exact code where you are loading and where you are checking

samwells
30 Oct 2008, 8:00 PM
This code i have taken from http://extjs.com/explorer/#xmlgrid

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.HttpProxy;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.XmlReader;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.http.client.RequestBuilder;

public class XmlGridExample extends LayoutContainer {
ListStore<ModelData> store ;
public XmlGridExample() {
setLayout(new FlowLayout(10));

List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
columns.add(new ColumnConfig("Sender", "Sender", 100));
columns.add(new ColumnConfig("Email", "Email", 165));
columns.add(new ColumnConfig("Phone", "Phone", 100));
columns.add(new ColumnConfig("State", "State", 50));
columns.add(new ColumnConfig("Zip", "Zip Code", 65));

// create the column model
ColumnModel cm = new ColumnModel(columns);

// defines the xml structure
ModelType type = new ModelType();
type.root = "records";
type.recordName = "record";
type.addField("Sender", "Name");
type.addField("Email");
type.addField("Phone");
type.addField("State");
type.addField("Zip");

// use a http proxy to get the data
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "data.xml");
HttpProxy proxy = new HttpProxy(builder);

// need a loader, proxy, and reader
XmlReader reader = new XmlReader(type);

final BaseListLoader loader = new BaseListLoader(proxy, reader);
store = new ListStore<ModelData>(loader);

// AT this place i want to get the store updated with the records
// I dont want the grid
// I tried below statements here also i m getting store count 0
// loader.load();
// System.out.println("Total Records : " + store.getCount());

final Grid grid = new Grid<ModelData>(store, cm);
grid.setBorders(true);
grid.setAutoExpandColumn("Sender");

ContentPanel panel = new ContentPanel();
panel.setFrame(true);
panel.setCollapsible(true);
panel.setAnimCollapse(false);
panel.setButtonAlign(HorizontalAlignment.CENTER);
panel.setIconStyle("icon-table");
panel.setHeading("XML Table Demo");
panel.setLayout(new FitLayout());
panel.add(grid);
panel.setSize(575, 350);

// add buttons
Button load = new Button("Load XML");
load.addSelectionListener(new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent ce) {

loader.load();
System.out.println("Total Records : " + store.getCount());
//Here also i m getting store count 0
}
});
panel.addButton(load);
add(panel);

}

}

The text in blue is the place where I am trying to print

I am just trying to print the store count in load button event
The table get updated but the store count is 0

And by the way my requirement is I don't want to update the grid
I don't have any grid component I just want to save the records in the store
so that i can use it for some other operation .

I am not able to find at which place does the store get updated with records
from xml

Sam Wells

gslender
30 Oct 2008, 10:29 PM
ok, you can't do that as loader.load is async :( I mentioned this before.

This is a GWT concept that you need to understand. The call doesn't happen ASAP like you are expecting - the code running happens like this...

1) loader.load is called and a HTTP request is sent (nothing comes back yet, we are waiting on the server)

2) you println("Total Records") etc and get zero as the HTTP hasn't returned any results

3) HTTP finally returns results and callback completes filling up the store.

BTW- This is not a ExtGWT problem as any GWT app requires this approach.

So the solution is that you need to plug into the callback and wait for the response.

To prove me right, call loader.load up before the button code, and in the button pressed, print out the store records total - this will work because the load will happen ASAP and in the time it takes you to press the button, the store would actually be full with a response. :D

samwells
31 Oct 2008, 3:45 AM
Thanks for the reply .
I got ur point I'll try to do as u said :-)

Algiano
24 Mar 2009, 3:25 PM
Now the interesting question is:

How do we know if the response returned zero results? This does not seem to be picked up by Loader.Load.

Thanks,
Ale

Algiano
24 Mar 2009, 3:39 PM
Also, I have my code in place to trigger the loading text when the data is being retrieved i.e.



listView.addListener(Events.Attach, new Listener<ComponentEvent>() {
public void handleEvent(ComponentEvent be) {
DeferredCommand.addCommand(new Command() {
public void execute() {
((PagingLoader) store.getLoader()).load(0, 25);
}
});
}
});


Which works as expected (i.e. the "loading..." text appears), however, if the store results are empty this continues to display the "Loading..." message which never gets removed. Unfortunately the ListView doesn't have an empty text method so how do I capture an empty store load and stop the loading message from appearing and setting the empty text?

Thanks,
Ale