View Full Version : [CLOSED] XmlReader Bugs RC2

30 Jun 2008, 11:38 AM
These might not technically be considered bugs, but design flaws. I'll post my solution. First the problems.

1.) Root node isn't being retrieved properly.
2.) TotalCount for results not parsing correctly if root node is wrong.
3.) Offset never being set.
4.) No XML documentation on XmlReader

1.) Root node isn't being retrieve properly. XmlReader line 43.

Node root = doc.getFirstChild();This looks like a no brainer. But if you have a doctype declaration like I did this screws you up pretty badly. Suggested solution is to do a getElementByName(modelType.root) like

Node root = doc.getElementsByTagName(modelType.root).item(0);This ensures the proper element is the root node.

2.) TotalCount for the results is not parsing correctly if the root node is wrong. XmlReader line 61.
If you pull the wrong root node then you'll never retrieve the correct total attribute. This is also a problem area because of the lack of documentation. Thanks for providing the source though. I was able to figure out that modelType.totalName was the attribute that was supposed to pull off the root element.

3.) Offset is never being set. This is a major problem for paging. XmlReader line 69.
The offset is always reset to 0. The loadConfig is passed in, I can't figure out why it is never being used to set the offset. I had to change the line to look like...

return new BasePagingLoadResult(records, ((PagingLoadConfig)loadConfig).getOffset(), totalCount)4.) Documentation. I couldn't find anything in the javadocs/explorer demo/or help center regarding the format for XML. In a nutshell if Model Type is defined like this...

ModelType mt = new ModelType();
mt.root = "vegetables";
mt.recordName = "carrot";
mt.totalName = "total";Then your xml should look like...

<vegetables total=34><carrots/><carrot/><carrot/></vegetables>Here's my redesigned read from XmlReader

public ListLoadResult read(C loadConfig, Object data) {
Document doc = XMLParser.parse((String) data);
Node root = doc.getElementsByTagName(modelType.root).item(0);
NodeList list = doc.getElementsByTagName(modelType.recordName);
ArrayList<BaseModel> records = new ArrayList<BaseModel>();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
Element elem = (Element) node;
BaseModel model = new BaseModel();
for (int j = 0; j < modelType.getFieldCount(); j++) {
DataField field = modelType.getField(j);
String map = field.map != null ? field.map : field.name;
String v = getValue(elem, map);
model.set(field.name, v);

int totalCount = records.size();

if (modelType.totalName != null) {
Node totalNode = root.getAttributes().getNamedItem(modelType.totalName);
if (totalNode != null) {
String sTot = totalNode.getNodeValue();
totalCount = Integer.parseInt(sTot);

return new BasePagingLoadResult(records, ((PagingLoadConfig)loadConfig).getOffset(), totalCount);

30 Jun 2008, 1:25 PM
Changes are in SVN.