1. #1
    Ext User
    Join Date
    Jul 2008
    Posts
    11
    Vote Rating
    0
    LarsDK is on a distinguished road

      0  

    Default XML Tree

    XML Tree


    Hi all,

    I simply wish to create a Tree that shows the entire content of an XML document. My problem is transferring the XML data to a List that later on will be used as a Store for my Tree. To illustrate my problem I made the simple example below:

    // Server side List generation
    //(UnitTreeData extends BaseTreeModel)

    List<UnitTreeData> root =
    new ArrayList<UnitTreeData>();

    UnitTreeData x =
    new UnitTreeData("number1");
    UnitTreeData y =
    new UnitTreeData("number2");
    UnitTreeData z =
    new UnitTreeData("number3");
    z.add(y);
    x.add(y);

    root.add(x);

    return root;

    /*********************/

    //client side code snippet

    Tree tree
    TreeLoader<UnitTreeData> loader =
    new BaseTreeLoader<UnitTreeData>(proxy);
    TreeStore<UnitTreeData> store =
    new TreeStore<UnitTreeData>(loader);
    TreeBinder<UnitTreeData> binder =
    new TreeBinder<UnitTreeData>(tree, store);
    binder.setDisplayProperty(
    "Name");
    loader.load();
    add(
    tree);


    /*********************/

    When the Tree is shown, it is displayed as

    number1
    -> number1
    -> number1
    -> [etc. eight child levels in total all named "number1" - item 9 is not loaded]

    Where I would have expected:

    number1
    -> number2
    -> number3

    Two questions:
    1) What is wrong with the way I add children to my root object?
    2) Is there a more simple way to transform XML data to a Store for a Tree?

    Thanks in advance.

    Lars

  2. #2
    Ext User
    Join Date
    Jul 2008
    Posts
    11
    Vote Rating
    0
    LarsDK is on a distinguished road

      0  

    Default Solved

    Solved


    As expected the solution was pretty simple.

    I changed the design a bit from the one inspired by the RpcProxy solution in the Helpcenter, to the Basic Tree design in the Explorer demo. This meant rather than having the server return a list of UnitTreeData, I now return an empty UnitTreeData root object by GWT RemoteService where I add the UnitTreeData object named "x".

    Then client-side I simply used the TreeBuilder:

    TreeBuilder.buildTree(tree, result);

    If someone has a smart solution to go populate a Tree with XML, I would appreciate a post of the solution. XML is a tree structure, so one would expect it to be simple.

    Lars

  3. #3
    Ext User
    Join Date
    Jul 2008
    Posts
    175
    Vote Rating
    0
    AmitOlsys is on a distinguished road

      0  

    Question I am also having this problem

    I am also having this problem


    Hi Dear !
    I am also having this problem could you please send me the solution if u hav done it smartly.
    Regards
    AmitOlsys

  4. #4
    Ext User
    Join Date
    Jul 2008
    Posts
    11
    Vote Rating
    0
    LarsDK is on a distinguished road

      0  

    Lightbulb Suggested xml to Tree approach

    Suggested xml to Tree approach


    This could be used as a starting point for parsing an xml tree into a TreeModel structure.

    The class is constructed with the xml document as a String. Afterwards you use the getBaseTreeModelData method to return the tree structure that is ready for the TreeBuilder:

    TreeBuilder.buildTree(tree, resultRPC);


    In this approach I add all xml element attributes as properties to the TreeModel object. I add the xml element name as a property as well named "xmlElementName". Since the toString() method determines the element names shown in the tree, the toString() method of the TreeModel are overridden to return the value of the "xmlElementName" property.

    This approach uses the JDOM API.
    All values are stored as Strings in the TreeModel object.

    Suggested improvements are welcome .

    Code

    The BaseTreeDataGenerator uses a wrapper class:

    Code:
    import java.io.Serializable;
    
    import com.extjs.gxt.ui.client.data.BaseTreeModel;
    import com.extjs.gxt.ui.client.data.TreeModel;
    
    public class SimpleBaseTreeModel extends BaseTreeModel<TreeModel<?>> implements Serializable {
    
        private static final long serialVersionUID = 5139514838451485551L;
    
        public SimpleBaseTreeModel() {
            super();
        }
        
        public String toString() {
            return (String) get("xmlElementName");
        }
    }
    The BaseTreeDataGenerator class:
    Code:
    import java.io.Reader;
    import java.io.StringReader;
    import java.util.List;
    
    import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    
    import SimpleBaseTreeModel;
    
    public class BaseTreeDataGenerator {
        
        private Document doc;
        
        public BaseTreeDataGenerator(String xmlDocument) {
            
            Reader reader = new StringReader(xmlDocument);
            SAXBuilder builder = new SAXBuilder();
            try {
                this.doc = builder.build(reader);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public SimpleBaseTreeModel getBaseTreeModelData () {
    
            SimpleBaseTreeModel root = new SimpleBaseTreeModel();
            appendChildren(doc.getRootElement(), root);
            return root;
        
        }
        
        
        @SuppressWarnings("unchecked")
        private void appendChildren(Element xmlElement, SimpleBaseTreeModel parent) {
            List list = xmlElement.getChildren();
            SimpleBaseTreeModel child;
    
            for (int i = 0; i < list.size(); i++) {
                Element xmlChild = (Element) list.get(i);
    
                child = new SimpleBaseTreeModel();
                
                child.set("xmlElementName", xmlChild.getName());
                
                
                List attributes = xmlChild.getAttributes();
                Attribute xmlAttribute;
                for(int j = 0; j < attributes.size(); j++) {
                    xmlAttribute = (Attribute)attributes.get(j);
                    child.set(xmlAttribute.getName(), xmlAttribute.getValue());
                }
                appendChildren(xmlChild, child);
                parent.add(child);
            }
        }
            
    }

Thread Participants: 1