XML and XMLList and XMLListCollection in Flex 3 - apache-flex

Please explain me what is the difference between XML and XMLList and XMLListCollection. If possible in simple words with example. Thanks in advance.

First, links to Flex 3 Language Reference - a must have bookmark for looking this stuff up.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
XML
XMLList
XMLListCollection
Secondly, I'd say check the Examples link for XMLList as that gives a good working example between the difference of XML and XMLList.
Use XML when you want to create a variable with a value of an XML document.
var mybooks:XML = <books>
<book>
<title>Book1</title>
</book>
<book>
<title>Book2</title>
</book>
</books>;
Use XMLList to create subsets of data from an XML variable.
var mybookTitles:XMLList = mybooks.title;
Finally, an XMLListCollection class is basically a helper class for taking your XML or XMLList object and using it in a control.
Take this snippet from mx.core.Repeater docs on its dataProvider property
If you set it to an XML or XMLList, it is converted into an XMLListCollection.
Hope this helps

An XMLListCollection is probably what you want to use. It has data binding and works well as a datasource in a datagrid.
An XMLListCollection is built from an XMLList. But after you have made an XMLListCollection from your XMLList, you rarely (if ever) use the XMLList again.
I rarely use XML. I think XML is mostly for backwards compatibility and that you are encouraged to use XMLList and XMLListCollection instead.

Related

extracting array of objects from xml response to js

so I'm extracting variables from my xml response and trying to reformat the objects but I want to do that the most efficient way possible. so I want to load the xml array of same objects into a js array that I can cycle thru and output the new format. I found a reference to type="nodeset" when extracting the XPath but i could not find a reference to it on the documentation.
what is the best way to load the full xml objects into a js variable and cycle thru the objects and output the new format
Thanks for any help you can give me on this.
Best way to accomplish this is with the XMLToJSON policy, a JavaScript callout in which you can mediate your payload, and then transform back with JSONToXML if you need it.
For an XML array that doesn't need filtering, you can use XPATH with type="nodeset", just as you described. This allows you to pull a node and all child nodes in a particular XPATH. As I'm sure you noticed, you can't do this by just extracting as type="string". Just know that you will need to convert the extracted variable to string before you can use the XML nodes like you do every other string. You can then do JSON.parse to take the string and manipulate the object like an array. The string conversion is as simple as calling a JS callout with the following code (if someone else has a better way, I'm all ears):
var extractedNodeSet = context.getVariable("extractedNodeSet");
var extractedNodeSetString = String(extractedNodeSet);
For an XML array that needs filtering/manipulation, I recommend to use XSLT along with the trusted <xsl:for-each select=...> element. This will let you set conditions on the XML array nodes, manipulate tags/data, and extract the data, all in one step. The only concern is that this isn't a JS array, so if you absolutely must have a JS array, then you'll need to then do an XMLtoJSON and work with the data from there.

How to convert TwitterResponse to something that ListView can read?

I've been googling for a really long time, and there is no example of how to display TwitterResponse object to something that datagrid or listview can display ... anyone has any solution to that ?
BTW, i tried to convert TwitterResponse to JSON Object, and itz just chaos ...
I've never used it, but it's clear from the documentation for Twitterizer that you need to bind to the ResponseObject property of the TwitterResponse object. If that is enumerable and contains properties then you should be able to bind to it.

What is source in ArrayCollection and XMLListCollection?

What is the role of source for an ArrayCollection or XMLListCollection?
Is it used just once - when constructing a new ArrayCollection or XMLListCollection object and is it copied to some internal data structure of that object?
Because nothing is updated when the source Array (or XMLList) is being modified and the documentation confirms it too:
The underlying XMLList for this collection. The XMLListCollection object does not represent any changes that you make directly to the source XMLList object. Always use the XMLListCollection methods to modify the collection.
This property can be used as the source for data binding. When this property is modified, it dispatches the listChanged event.
I'm asking because Flex examples related to dataProviders always use some Array or XMLList as source of data for a data-driven component. And I wonder, if using Array or XMLList is necessary at all - when for example loading data from external PHP-script.
Using collections ArrayCollection or XMLListCollection, you can apply sorting or filter to them. In this case source will return all the elements in original order without applying filter. I often use source this way. Adding and removing items from collection also modifies original source array.
What about using collections or arrays as data provider, you can use them all in MX lists but Spark lists can only accept IList's which implemented by collections mentioned above.
The advantage of using collections as data provider is in possibility to apply filters and sorting without modifying of original array. And of course possibility to listen collection's changes.
Using pure Array or XMLList in samples, I suppose, is for simplicity and some implementation details of particular client-server interaction.

Flex HTTPService Resultformat

What is the real difference between these resultformats for HTTPService in Flex :
text
object
xml
e4x
Especially, the last three seem pretty close to each other by their description.
from the manual of HTTPService:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/rpc/http/HTTPService.html#resultFormat
object: The value returned is XML and is parsed as a tree of ActionScript objects. This is the default.
array: The value returned is XML and is parsed as a tree of ActionScript objects however if the top level object is not an Array, a new Array is created and the result set as the first item. If makeObjectsBindable is true then the Array will be wrapped in an ArrayCollection.
xml: The value returned is XML and is returned as literal XML in an ActionScript XMLnode object.
flashvars: The value returned is text containing name=value pairs separated by ampersands, which is parsed into an ActionScript object.
text: The value returned is text, and is left raw.
e4x: The value returned is XML and is returned as literal XML in an ActionScript XML object, which can be accessed using ECMAScript for XML (E4X) expressions.
The classtype of the returned object differs.
text => String
object => A generic object that you can use like a hash
e4x => an object of type XML
xml => I forget... a String?
I recently had some issues with the "object" and "e4x" resultFormat.
I have a base WebService Class that I use for sending requests and receiving results. By default, all results come back as "object". However, sometimes Flex looks at the data, and converts it to an appropriate type. For instance, if you have an XML result that looks like the following, it will convert it to an Array Object (not sure why...but...):
<root>
<child>text</child>
<child>text text</child>
</root>
Now, an Array Object like this can easily be cast as XML, since, as a string it is also XML.
However, some XML documents are returned as an ObjectProxy, which cannot be cast as XML, when the resultFormat is "object".
I tried using "e4x", as it was suggested here, but then I ran into problems with namespaces not being preserved correctly.
I finally tried "xml", and I am getting the expected results. It's interesting that when you inspect the event result property using the Flex Debugger, it is said to be an Array, even when you specify a resultFormat of "xml". I guess this allows for easily casting to ArrayCollection...not sure....

Does a "tag search" method exist working with RDF/XML documents in Flex?

I'm working on a RDF file in my application. I loaded it with the URLLoader and I've imported flash.xml.* class in order to manipulate the content of my RDF file. I need to count certain tags ("Description") in an efficient way. The number of the tags I'm searching for is variable and now I'm using a combination of methods firstChild, lastChild, nextSibling in while cicles in order to take the informations I need from the document. My question is if there's a more direct and general "search method" that helps me avoid that complex and not elegant search that I made.
The syntax #Property is used to directly access a Tag's property, right? Do you think that it could be useful for me to solve my problem?
Thank you all for the help
David
Sure. Try this:
// assuming xml is the XML object containing your document
var numberOfDescriptionElements:int = xml..Description.length();
In general, you should check out the details of E4X (ECMAScript for XML), a very handy and consise sub-language of ActionScript to access XML structures.

Resources