.. _dojox/data/XmlStore: dojox.data.XmlStore =================== :Status: Contributed, Draft :Version: 1.0 :Author: Jared Jurkiewicz :Available: since V1.0 .. contents:: :depth: 3 XmlStore is a store provided by Dojo that is contained in the DojoX project. XmlStore is a read and write interface to basic XML data. XML is a common data interchange format and a store that can work with fairly generic XML documents is useful. The store is designed so that you can over-ride certain functions to get specific behaviors to occur when performing reads and saves. ============== Supported APIs ============== The following dojo.data APIs are implemented by XmlStore * :ref:`dojo.data.api.Read ` * :ref:`dojo.data.api.Write ` * :ref:`dojo.data.api.Identity ` * **New for dojo 1.3!** Identity API has been implemented. The store wil return xpath for an item, or it will return the value of a key attribute you specified. Specifying keyAttribute is not needed for client-side query and filtering case, but it is required for server-side query handling and update. ================= Example XML Input ================= The store is designed so that it can read generic XML and present back nodes as dojo.data items. The following is an example of an XML document that this store can read: :: 1 Title of 1 Author of 1 2 Title of 2 Author of 2 3 Title of 3 Author of 3 4 Title of 4 Author of 4 5 Title of 5 Author of 5 6 Title of 6 Author of 6 ... ====================== Constructor Parameters ====================== The constructor for XmlStore takes the following possible parameters in its keyword arguments: +--------------------------+--------------------------------------------------------------------------------------------------------+ |**Parameter** |**Description** | +--------------------------+--------------------------------------------------------------------------------------------------------+ |url |The URL from which to load the XML file containing the data. This URL is also the end point used for | | |posting data base in a save. This is optional. | +--------------------------+--------------------------------------------------------------------------------------------------------+ |sendQuery |Boolean option whether or not to send the query to a server for processing. The default is false. | | | | | |false | | | It is assumed the server sends back the entire store dataset and the filtering and sorting must | | | occur on the client side. | | | | | |true | | | It is assumed the server is handling the filtering and is only sending back XML nodes that match the | | | query. No filtering occurs clientside. | +--------------------------+--------------------------------------------------------------------------------------------------------+ |rootItem |A tag name for root items. This is optional. If it is not provided, then the XmlStore assumes the tags | | |under the root element of the document are the root items. | +--------------------------+--------------------------------------------------------------------------------------------------------+ |keyAttribute |An attribute name for a key or an identity. This is optional unless server-side query handling is used. | +--------------------------+--------------------------------------------------------------------------------------------------------+ |attributeMap |An anonymous object that contains properties for attribute mapping, for example | | |{"tag_name.item_attribute_name": "@xml_attribute_name", ...}. This is optional. This is done so that | | |attributes which are actual XML tag attributes (and not sub-tags of an XML tag), can be referenced. | +--------------------------+--------------------------------------------------------------------------------------------------------+ |label |The attribute of an item to use for the return of getLabel(). This is optional. | +--------------------------+--------------------------------------------------------------------------------------------------------+ | urlPreventCache |This boolean value controls whether dojo attempts to prevent the browser caching mechanism from | | **(since dojo 1.4)** |caching values loaded from a webserver. The default is true for backwards compatibility | +--------------------------+--------------------------------------------------------------------------------------------------------+ ================================= User-provided function over-rides ================================= The following functions can be over-ridden to alter save behavior, as described: _getPostUrl(item) Function that can be over-ridden to alter the way the store POSTs new items to the service. Note that this follows the REST convention in which an HTTP POST is a creation of a new resource. _getPutUrl(item) Function that can be over-ridden to alter the way the store PUTs updated items to the service. Note that this follows the REST convention in which an HTTP PUT is an update of an existing resource. _getDeleteUrl(item) Function that can be over-ridden to alter the way the store sends a DELETE item to the service. Note that this follows the REST convention in which an HTTP DELETE is used to remove a resource. ============ Query Syntax ============ The fetch method query syntax for XmlStore is simple and straightforward. It allows for a list of attributes to match against in an AND fashion, just like :ref:`dojo.data.ItemFileReadStore `. For example, the following query object will locate all items that have attributes of those names that match both of those values: :: { foo:"bar", bit:"bite"} Note that XmlStore supports the use of wild cards (multi-character * and single character ?) in its attribute value matching. Examples To find all items with attribute foo that start with bar, the query would be: :: { foo:"bar*"} To find all items with attribute foo the value of which ends with ar and ignoring only the first character, the query would be: :: { foo:"?ar"} **NOTE:** Other stores should follow the same query definition semantics for consistency. ============= Query Options ============= Dojo.data defines support for a 'queryOptions' modifier object that affects the behavior of the query. The two defined options listed by the API are *ignoreCase* and *deep*. dojox.data.XmlStore supports these options. The affect of these options on a query is defined below. +------------+------------------------------------------------------------------------------------------------------------------------+ | **Option** | **Result** | +------------+------------------------------------------------------------------------------------------------------------------------+ | ignoreCase |The default is **false**. When set to true, the match on attributes is done in a case-insensitive fashion. This means | | |with ignoreCase: true, a query of A* would match *Apple* and *acorn* | +------------+------------------------------------------------------------------------------------------------------------------------+ | deep |This option affects searching when the structure passed to ItemFileReadStore has hierarchy. For an example of that, | | |refer to *Item Structure Example: Items with Hierarchy*. the default value for this option is false, which means the | | |query is only applied against root items in the tree of data items. If it is set to true, then the query is applied to | | |root items and *all* child data items. Think of it as a recursive search. | +------------+------------------------------------------------------------------------------------------------------------------------+ ============== Usage Examples ============== For these examples, we'll assume a data source as defined by the example data format in this page. Query for all books that start with ISBN: A9B57 ----------------------------------------------- .. code-block :: javascript var store = new dojox.data.XmlStore({url: "books.xml", rootItem: "book"}); var gotBooks = function(items, request){ for (var i = 0; i < items.length; i++){ var item = items[i]; console.log("Located book: " + store.getValue(item, "title"); } } var request = store.fetch({query: {isbn:"A9B57*"}, onComplete: gotBooks}); Query for all books that start with ISBN: A9B57 Case insensitively ------------------------------------------------------------------ .. code-block :: javascript var store = new dojox.data.XmlStore({url: "books.xml", rootItem: "book"}); var gotBooks = function(items, request){ for (var i = 0; i < items.length; i++){ var item = items[i]; console.log("Located book: " + store.getValue(item, "title"); } } var request = store.fetch({query: {isbn:"a9b57*"}, queryOptions: {ignoreCase: true}, onComplete: gotBooks}); ========================= Functional Usage Examples ========================= Searching node attributes ------------------------- .. cv-compound :: .. cv :: javascript .. cv :: html ISBN:

Click to search!


Query used:
Books located:
Connecting XmlStore to dijit.form.ComboBox ------------------------------------------ .. cv-compound :: .. cv :: javascript .. cv :: html
Connecting XmlStore to dojox.grid.DataGrid ------------------------------------------ .. cv-compound :: .. cv :: javascript .. cv :: html
.. cv:: css