Create grid dynamicaly from xml

Hello everybody,
I have to create a grid from an xml. The problem is that the coloumns have to be create dynamically from the xml (because depending on the request, the xml result can be different). for example given this xml:

<data>
	<item id="1">
		<country>China</country>
		<population>1 341 990 000</population>
		<year>2011</year>
	</item>
	<item id="4">
		<country>Indonesia</country>
		<population>237 556 363</population>
		<year>2010</year>
	</item>
</data>

the result should be:

|country   |population   |year|
-------------------------------
-------------------------------
|China     |1 341 990 000|2011|
-------------------------------
|Indonesia |237 556 363  |2011|
-------------------------------

there is any way to do it directly from the component? in case of No how can I manage it?

Thanks in advance for your help
Danilo

There is no built-in initialization from the xml, but it possible to do the next

a) load data by dhx.ajax
b) now you have access to the server response ( both as plain text and as json object ) and can get some of that info and use for grid’s configuration
c) second part of data can be used in parse command to load the data

Something like next ( beware of typos :slight_smile: )

dhx.ajax("data.json", function(text){
      var resp = dhx.DataDriver.json.toObject(text);
      $$('grid').define("header", resp.partA);
      $$('grid').parse(resp.partB);
});

Hello Stanislav,
first of all thanks for your answer. But I have some problem in implementig what you said. I’m using XML for data representation, so I defined the grid component as:

view:'grid',
scroll:true,
id:'ui_grid',
select:'multiselect'',
datatype:'xml'

when the component is initialized I call the method you said in your answer:

dhx.ajax("http://192.168.2.80:8089/EdicomStuffServer/services/OutsmonRest/getDomains/username/prueba/password/prueba/", function(text,xml){
	var resp = dhx.DataDriver.xml.toObject(text,xml);
	......
});

inspecting the resp variable with firebug (I see that this variable is a Document object) I do not see the representation of the xml document (the link http:\192… work perfectly and return an xml as showed in the first post), where can I find a specification for the Document object?

The other function:

$$('ui_grid').define("header", resp.partA);

the docs specification for the component grid says that “header” shows/hides grid header but I should create the fields for the component so the first parameter of the $$(‘ui_grid’).define should be “field”. Seeing that the field name has the same name of the xml tags, I need to get them from the resp variable (an array of all the tags in the Xml document). How can I do that?

Thanks again for your help

Sorry for inconvenience, I have provided the sample with json which was misleading .

toObject for xml data type returns an XML Document object
This is common XML object, not a dhtmlx component|wrapper. You can use W3C DOM api to access its nodes and attributes

[code]dhx.ajax(“data.xml”, function(text,xml){
var driver = dhx.DataDriver.xml;
var xml = driver.toObject(text, xml);

  var top_node  = driver.xpath("//nodename");
  var json_object = diver.tagToObject(top_node);
  //here you will have json object with all properties of xml
  //you can use xpath with //topnode/fieldnode - to select the necessary branch in xml
  //after converting to json - you can feed result to the define or parse commands

});
[/code]