I have an XML response that I’d like to bind and display to a GXT grid.
The basic example I’ve found online says to do the following:
// defines the xml structure
ModelType type = new ModelType();
type.setRoot("records");
type.setRecordName("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,
GWT.getHostPageBaseURL() + "data/data.xml");
HttpProxy<String> proxy = new HttpProxy<String>(builder);
// need a loader, proxy, and reader
XmlLoadResultReader<ListLoadResult<ModelData>> reader = new XmlLoadResultReader<ListLoadResult<ModelData>>(
type);
final BaseListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(
proxy, reader);
ListStore<ModelData> store = new ListStore<ModelData>(loader);
This works just fine if your XML is in a simple structure (ie no nested elements).
However, my XML is more like this:
<myRoot>
<myElement>
<first>
<time></time>
<place></place>
</first>
<second>
<time></time>
<place></place>
</second>
<third>
<time></time>
<place></place>
</third>
</myElement>
...
</myRoot>
How can I represent this using ModelType in order for the Grid to properly display the results?
Abstract
The key is how you parse the XML document to build the
ModelData(extended here asModelType).GXT showcase shows how to parse their example XML into beans, and you can use your favored XML parser to do the same.
here is a snippet from their repositories, edited based on your XML data to demonstrate how it’s done:
Code Example
Service Implementation (with parser)
you can, of course, extend this technique further (continue traversing deeper as you like) in order to match your needs.