Parse XML data into a HierarchicalContainer

Hey,

Im starting to work with XML and Containers in Vaadin , I want to parse some XML data into an HierarchicalContainer to build an tree with it later , is there an example of how this may be done ?

Any help is greatly Appreciated.

I don’t think there are any ready-made tools for this, so I’m afraid you’ll have quite lot of manual work ahead of you. What you can do, is parse the XML with, for example, using the
XOM
library and then manually populate the HierarchicalContainer. Depending on your use case, another option could be to make classes representing your XML structure, then use tools such as
jaxb
or
castor
to map your XML to objects. After this, you can manually populate the container using the objects.

To get the data i want out of my XML (MSDL) is not so hard , my problem is to fill the data to the HierarchicalContainer , I use the SAX libary to parse + store the data i get. So i was looking for an example how to handle the data i already have , like the hardware container from the vaadin demo site.

A HierarchicalContainer is populated in the same way as any other container, in other words, by calling addItem() and setting the appropriate property values (see
Book of Vaadin
for more details about containers and how to populate them).

The next step is to define the parent/child relations in the container. This is done by calling the setParent(Object childItemId, Object parentItemId) method in the container. Below is a simple example that demonstrates the defining of a hierarchy.

HierarchicalContainer container = new HierarchicalContainer();
container.addItem("Europe");
container.addItem("North America");
container.addItem("Asia");

container.addItem("Finland");
container.setParent("Finland", "Europe");

container.addItem("USA");
container.setParent("USA", "North America");

container.addItem("China");
container.setParent("China", "Asia");

In the HardwareContainer example there is a line that I dont understand (dont know what it does).
I have looked in to the Book of Vaadin but I couldnt find an explanation that would clear things up for me , i think thats the main problem at the moment i have . :

    public static final Object hw_PROPERTY_NAME = "name";
hwContainer.addContainerProperty(hw_PROPERTY_NAME, String.class, null);

The data model in Vaadin consists of properties, items and containers. Properties could be simplified as a key-value pair. An item could be considered as a collection of properties while a container is a collection of items. If we think about these in database terms, a container would be the equivalent for a database table, an item would be a row in the table and a property would be a column in the table.

When you create a HierarchicalContainer, by default, it doesn’t have any properties defined. What the second line does in your example, is that defines a property to to the container. The addContainerProperty method takes three parameters, a property id, property type and a default value. In your example, the property id is the hw_PROPERTY_NAME object, namely, a string called “name”. The second property defines that the name property is of type String and the third property defines, that if a property value is not set, then null is used as the default value.

If we look at my previous example, it could be modified so, that instead of a string representing the country name, I use an object called Country. The class as a field named “name” which I’ll then add to my container as a property.

HierarchicalContainer container = new HierarchicalContainer();
container.addContainerProperty("name", String.class, null);

container.addItem("Europe");
container.addItem("North America");
container.addItem("Asia");

Country country = new Country("Finland");
Item item = container.addItem(country);
item.getItemProperty("name").setValue(country.getName());
container.setParent(country, "Europe");
....

Add this container to, for example, a TreeTable to see how it looks like in practice.

There’s a bad link in your post. Castor is at
http://castor.codehaus.org/
.

Awesome tutorial here, thanks for sharing, but what do you guys think about Back4app as the best[ Parse ]
(https://www.back4app.com )Alternative for developing scalable and extensible applications? Is anyone using the platform?