PDA

View Full Version : How to Read/Parse XML Synchronously


jmhwhite2001
12-04-2008, 09:06 AM
Very simple. I need example code of how to parse an xml file and use what's been parsed. I'm not looking for a discussion why/how/pro/cons. I just want example code.

Now, from what I read, things happen asynchronously, yet I need to be able to merely parse through an xml synchronously. Data is stored in an xml file that I need to parse through.

What I want to do with the data is irrelevant. Anybody got any suggestions?

Essentially, get the xml data and store it. Then use what's been stored to do what I need to do.

jpnet
12-05-2008, 01:12 PM
There are a couple of unknowns yet...
1) How do you want to store it? In a variable? In a database?
2) Maybe I misunderstood... but what do you mean by get the XML data synchronously? Is the data actually in an XML file that resides on your hard drive? Will it be coming from an HTTP request? When it comes to parsing a string of XML to a data structure, for all intents and purposes it is in a synchronous manner. Could it be that you were reading about AJAX (Asynchronous Javascript and XML) and thought XML parsing was asynchronous? :-?

-JP

jmhwhite2001
12-05-2008, 01:15 PM
Ok, the data currently for testing purposes will come from an xml file stored locally within my development environment.

In the future, the xml will be served by an HTTP request to a JSP page.

Thanks!

jmhwhite2001
12-05-2008, 01:17 PM
In regards to how it's stored in the application, then a Datastore is fine. I can always navigate it like a list.

Thanks.

karacutey
12-06-2008, 12:53 AM
this works for us, its made from an example i found online, it currently is embedded into a grid panel that is displayed in a tab panel.

package com.bluestone.client.layout.grids;

/**
*
* @author john
*/

import com.bluestone.client.Util;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.*;
import com.extjs.gxt.ui.client.store.*;
import com.extjs.gxt.ui.client.widget.*;
import com.extjs.gxt.ui.client.widget.grid.*;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.http.client.RequestBuilder;
import java.util.Iterator;

public class XMLGridPanel extends ContentPanel {

/************************************
* GETTERS/SETTERS ATTRIBUTES
************************************/
protected Grid m_grid;
protected String m_gridId;
protected ListStore<ModelData> m_listStore;
protected ColumnModel m_columnModel;
protected PagingLoader m_pagingLoader;
protected RequestBuilder m_requestBuilder;
protected HttpProxy<Object, PagingLoadResult<ModelData>> m_httpProxy;
protected ModelType m_modelType;
protected XmlReader<PagingLoadConfig> m_xmlReader;
protected String m_requestUrl;
protected int m_pageSize;
protected int m_offset;
protected Boolean m_isVisible;
protected Boolean m_isFramed = false; //default value

/************************************
* CONSTRUCTOR(S)
************************************/
public XMLGridPanel() {
m_pageSize = 50;
m_offset = 0;
m_requestUrl = "";
m_isVisible = false;
m_gridId = "xmlGridPanel";

if (m_isFramed) {
this.setFrame(true);
this.setCollapsible(true);
this.setAnimCollapse(false);
this.setButtonAlign(HorizontalAlignment.CENTER);
this.setIconStyle("icon-table");
this.setHeading("Paging Grid");
this.setLayout(new FitLayout());
} else {
this.setFrame(false);
this.setCollapsible(false);
this.setAnimCollapse(false);
this.setHeaderVisible(false);
this.setLayout(new FitLayout());
}
}

/************************************
* GETTERS/SETTERS METHODS
************************************/
public Grid getGrid() {
return m_grid;
}

public ModelType getModelType() {
return m_modelType;
}

public int getOffset() {
return m_offset;
}

public int getPageSize() {
return m_pageSize;
}

public String getRequestUrl() {
return m_requestUrl;
}

public void setGridId(String id) {
m_gridId = id;
}

public void setColumnModel(ColumnModel columnModel) {
m_columnModel = columnModel;
}

public void setModelType(ModelType modelType) {
m_modelType = modelType;
}

public void setPageSize(int pageSize) {
m_pageSize = pageSize;
}

public void setOffset(int offset) {
m_offset = offset;
}

public void setRequestUrl(String url) {
m_requestUrl = url;
m_requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
m_httpProxy = new HttpProxy<Object, PagingLoadResult<ModelData>>(m_requestBuilder);
}

/************************************
* PUBLIC METHODS
************************************/
public void hideFrame() {
m_isFramed = false;
}

public void showFrame() {
m_isFramed = true;
}

public void load() {
this.load(m_offset, m_pageSize);
}

public void load(int offset, int pageSize) {
m_pagingLoader.load(offset, pageSize);
}

@Override
public void show() {
if (m_isVisible) {
throw new RuntimeException("GridPanel is already visible.");
}

m_xmlReader = new XmlReader<PagingLoadConfig>(m_modelType) {

@Override
protected ListLoadResult newLoadResult(PagingLoadConfig loadConfig, List<ModelData> models) {
PagingLoadResult results = new BasePagingLoadResult(models, loadConfig.getOffset(), loadConfig.getLimit());

for (Iterator<ModelData> it = results.getData().iterator(); it.hasNext();) {
ModelData resultData = it.next();

if (resultData.get("when") != null) {
resultData.set("date", Util.formatUnixTimeToDateString(resultData.get("when").toString()));

resultData.set("time", Util.formatUnixTimeToTimeString(resultData.get("when").toString()));
}

if (resultData.get("since") != null) {
resultData.set("since", Util.formatUnixTimeToTimeString(resultData.get("when").toString()));
}
}

return results;
}
};

m_pagingLoader = new BasePagingLoader(m_httpProxy, m_xmlReader);

ListStore<ModelData> listStore = new ListStore<ModelData>(m_pagingLoader);

final PagingToolBar toolbar = new PagingToolBar(m_pageSize);
toolbar.bind(m_pagingLoader);

m_grid = new Grid(listStore, m_columnModel);
m_grid.setLoadMask(true);
m_grid.setId(m_gridId);

this.add(m_grid);
this.setBottomComponent(toolbar);

m_isVisible = true;
}
}



look at the xml reader class. im sure you can use something else besides pagingload data thing that stores the results from the async call. its pretty nifty cuz its dynamic based on the model you create for it. you could easily make a generic helper class to handle this xmlread. cheers

jpnet
12-06-2008, 03:18 AM
Hehe... looks like my modified example [http://extjs.com/forum/showthread.php?t=47906] :D

Hope it's useful.

-JP

karacutey
12-08-2008, 02:34 PM
yeah it was, we have done quite a bit with it on our current project. mainly creating a XMLreader interface that can be implement on any component that uses any type of data store. works nicely

jmhwhite2001
12-08-2008, 02:52 PM
Thanks for the response. I'm already using this code for my grid. However, I need to be able to read xml for properties as well as use it to create my own tree. From what I read, you can only read xml asynchronously and that's not gonna get it.

jmhwhite2001
12-08-2008, 02:52 PM
I still haven't tried the modifications you added myself, but working on it.

karacutey
12-08-2008, 04:39 PM
as far as getting it to work with reading properties, dunno how, its just easier for us to return back xml with no properties. xquery is such a nice language ^_^

i would really like to see someone make a simple xml gwt module that would be nice, i dont like the existing modules for xml handling. you could always just get the string back thats the xml and just manually iterate and build your object store then pass that into your tree constructor