0

I am ExtJS for GUI development. I am using XML Store for retrieving the data from server.

My XML looks like this.

<meta>
 <entry>x</entry>
 <entry>Y</entry>
</meta>
<data>
  <value>100</value>
  <value>500</value>
 </data>

Where X=100 and Y=500

How do i retrieve the data and value from this using XMLStore?

Anandan
  • 983
  • 3
  • 13
  • 28
  • Are you sure this is the XML structure you want ? The correlation between the metadata and the data is flaky ... How does it look with multiple data records ? (If there aren't multiple entries the need for a data store is questionable) – ob1 Jul 05 '10 at 07:59
  • I am actually getting the XML data from another tool. I can't change the XML format.Also the number of field "entry" and field "value" are fixed.They are equal in number always. – Anandan Jul 05 '10 at 08:30
  • So why do you need it in a data store ? – ob1 Jul 05 '10 at 08:39
  • Do you mean you want to have a store with 2 fields: "entry" and "value", and you can have 2 pairs like this as in your example or more than 2 ? – ob1 Jul 05 '10 at 08:45

1 Answers1

1

Since the XML structure is not really suitable for what the XML Store/Reader expect, I suggest you parse the XML yourself into a more standard format and then load the data to a JsonStore for example.

Parsing code: (wrote it off the top of my head, so might need some adjustments ...)

var data = [];
var q = Ext.DomQuery;
var entries = q.select('meta > entry', theXML);
var values = q.select('data > value', theXML);
for (var i = 0; i < entries.length; ++i) {
    var recordData = {
        entry: entries[i].firstChild.nodeValue,
        value: values[i].firstChild.nodeValue
    }
    data.push(recordData);
}

Hope it will be useful to you ...

ob1
  • 1,721
  • 10
  • 25
  • Thanks for your reply. Is the "theXML" refers to the location of the XML file? In my case, it is exported through a REST API. Is it possible to use the URI in this code instead of the XML file path? – Anandan Jul 05 '10 at 11:58
  • The "theXML" is the XML DOM document (or the document root node object). You can get one by doing an AJAX call to your REST API URL and accessing *response.responseXML* on the success callback – ob1 Jul 05 '10 at 12:27