This is the personal weblog of Greg Burch.

XML 2 DataProvider

Here is some useful (maybe) code I tossed together. It takes XML and forms it into a dataprovider. If you have a label attribute in your node then that will show up as the label in the item. The function signiture is:

XML.XML2DP( startNode, dataProvider );

The dataproviders "data" property will hold the following:

attributes: the attribute object of the xml children: the child nodes of the node

An example usage:

doc_xml = new XML("<root label = "root"><child1 label = "CHILD1"><c1c1 label = "Child 1's first child" /><c1c2 label = "Child 1's second child" /></child1><child2 label="CHILD2"><c2c1 label = "Child 2's first child" /><c2c2 label = "Child 2's second child" /></child2></root>");
dp = new DataProviderClass();
XML.XML2DP(doc_xml.childNodes[0].childNodes[0], dp);
myListBox.setDataProvider(dp);
That will fill my list box with items like:
Child1's First Child
Child1's Second Child
If you just passed in
doc_xml.childNodes[0]
You would get:
Child1
Child1's First Child
Child1's Second Child
Child2
Child2's First Child
Child2's Second Child

etc...

XML2DP.as

I haven't used this too much in the real world yet but I see a need for it in the future. Let me know of any suggestions/comments.



© Copyright 2003 Greg Burch. Click here to send an email to the editor of this weblog.
Last update: 2/13/2003; 2:38:08 PM.