Flex Actionscript - loop through xml find if at least one checked - apache-flex

I have an xml as a string in flex in the below format.
I can have as many nested groups and test as possible.
I need a way to find which are checked, how can I iterate through this?
<node label="All" checked="0">
<node label="Group1" checked="0">
<node label="Test1" checked="0" />
<node label="Test2" checked="0" />
<node label="Group1Inner" checked="0">
<node label="Test1Inner" checked="0" />
<node label="Test2Inner" checked="0" />
</node>
<node label="Group2Inner" checked="0">
<node label="Test1Inner" checked="0" />
<node label="Test2Inner" checked="0" />
</node>
</node>
</node>

You can manage this by using E4X
var xx:XML = <node label="All" checked="0">
<node label="Group1" checked="0">
<node label="Test1" checked="0" />
<node label="Test2" checked="0" />
<node label="Group1Inner" checked="0">
<node label="Test1Inner" checked="0" />
<node label="Test2Inner" checked="0" />
</node>
<node label="Group2Inner" checked="0">
<node label="Test1Inner" checked="0" />
<node label="Test2Inner" checked="0" />
</node>
</node>
</node>;
trace(xx..node.(#checked==0))

Related

improving code to find nodes with using attributename and node

I have following xml:
<TextWithNodes><Node id="0" />astralis<Node id="8" /> <Node id="9" />ltd<Node id="12" />
<Node id="14" />{<Node id="15" />DOCUMENT<Node id="23" />}<Node id="24" /> <Node id="25" />{<Node id="26" />TYPE<Node id="30" />}<Node id="31" />EX-<Node id="34" />10<Node id="36" />.<Node id="37" />12<Node id="39" /> <Node id="40" />{<Node id="41" />SEQUENCE<Node id="49" />}<Node id="50" />3<Node id="51" /> <Node id="52" />{<Node id="53" />FILENAME<Node id="61" />}<Node id="62" />e<Node id="63" />300201<Node id="69" />_<Node id="70" />ex<Node id="72" />10<Node id="74" />-<Node id="75" />12<Node id="77" />.<Node id="78" />txt<Node id="81" /> </TextWithNodes>
and I need to pick node from Id 25 to id 75. It is a portion of XML. Original XML is very long.
I am using following code:
Dim reader As XmlTextReader = New XmlTextReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/gate_xml_output.xml"))
reader.WhitespaceHandling = WhitespaceHandling.None
Dim xmlDoc As XmlDocument = New XmlDocument()
'Load the file into the XmlDocument
xmlDoc.Load(reader)
'Close off the connection to the file.
reader.Close()
Dim nodeList As XmlNodeList = xmlDoc.SelectNodes("//TextWithNodes/node()[preceding-sibling::Node[#id=" & startNode & "] and following-sibling::Node[#id=" & endNode & "]]")
Dim sb As StringBuilder = New StringBuilder
For Each childNode As XmlNode In nodeList
If childNode.Value IsNot Nothing Then
sb.Append(childNode.Value & " ")
End If
Next
' read the text between these nodes
ExtractText = sb.ToString
It is working but It is very slow. Any alternative of getting this data from XML ?
Please suggest.
Thanks
Explore Linq to XML; it should be quicker: http://msdn.microsoft.com/en-us/library/bb387098.aspx
Also good info here: http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
There's ton of info out there.
Here's an article about performance comparison with xmldoc and xmlreader: http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html
and another: http://msdn.microsoft.com/en-us/library/bb387048.aspx
+1
You could do it with XPath. Like this:
Dim sb As New StringBuilder()
Dim document As New XPathDocument("C:\testfile2.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim iterator As XPathNodeIterator = navigator.Select("yourPath")
While iterator.MoveNext()
sb.Append(iterator.Current.Value)
End While
You can find more information here
The evaluation of this XPath expression has only O(N) complexity (linear) vs. O(N^2) for your original XPath expression:
/*/Node[#id >= 15 and not(#id > 75)]
This selects any Node element that is a child of the top element of the XML document and the value of whose id attribute is between the two specified lower and upper limits: respectively 15 and 75.
Here we assume that the values of the id attributes of Node elements are monotonously increasing -- exactly as in the provided XML document.
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/Node[#id >= 15 and not(#id > 75)]"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is performed on the provided XML document:
<TextWithNodes>
<Node id="0" />astralis
<Node id="8" />
<Node id="9" />ltd
<Node id="12" />
<Node id="14" />{
<Node id="15" />DOCUMENT
<Node id="23" />}
<Node id="24" />
<Node id="25" />{
<Node id="26" />TYPE
<Node id="30" />}
<Node id="31" />EX-
<Node id="34" />10
<Node id="36" />.
<Node id="37" />12
<Node id="39" />
<Node id="40" />{
<Node id="41" />SEQUENCE
<Node id="49" />}
<Node id="50" />3
<Node id="51" />
<Node id="52" />{
<Node id="53" />FILENAME
<Node id="61" />}
<Node id="62" />e
<Node id="63" />300201
<Node id="69" />_
<Node id="70" />ex
<Node id="72" />10
<Node id="74" />-
<Node id="75" />12
<Node id="77" />.
<Node id="78" />txt
<Node id="81" />
</TextWithNodes>
the XPath expression is evaluated and the selected nodes are output:
<Node id="15"/>
<Node id="23"/>
<Node id="24"/>
<Node id="25"/>
<Node id="26"/>
<Node id="30"/>
<Node id="31"/>
<Node id="34"/>
<Node id="36"/>
<Node id="37"/>
<Node id="39"/>
<Node id="40"/>
<Node id="41"/>
<Node id="49"/>
<Node id="50"/>
<Node id="51"/>
<Node id="52"/>
<Node id="53"/>
<Node id="61"/>
<Node id="62"/>
<Node id="63"/>
<Node id="69"/>
<Node id="70"/>
<Node id="72"/>
<Node id="74"/>
<Node id="75"/>

Flex 4 Custom Scroll bar on tree component

I want to customize the scrollbars on a mx|Tree component is Flex 4.
I would like to mimic this functionality: http://flexponential.com/2009/10/09/changing-the-position-of-the-scroll-bars-in-a-spark-list/
Any thoughts or reccomendations?
This can work, add your own error checking/styling, but you get the idea:
<s:VGroup gap="-1">
<s:Button width="100%" label="Scroll up" click="{tree.verticalScrollPosition--}" />
<mx:Tree id="tree" labelField="#label" showRoot="false" width="300" height="150"
verticalScrollPolicy="off">
<mx:dataProvider>
<fx:XML>
<root>
<node label="Parent 1">
<node label="Child 1" />
<node label="Child 2">
<node label="Grandchild 1" />
<node label="Grandchild 2" />
</node>
<node label="Child 3" />
<node label="Child 4" />
<node label="Child 5" />
<node label="Child 6" />
<node label="Child 7" />
<node label="Child 8" />
<node label="Child 9" />
</node>
</root>
</fx:XML>
</mx:dataProvider>
</mx:Tree>
<s:Button width="100%" label="Scroll Down" click="{tree.verticalScrollPosition++}"/>
</s:VGroup>
you could also try to use verticalScrollBarStyleName/horizontalScrollBarStyleName with a css style. But ofc it lacks the comfort of spark skins. ;)

How can I provide links to items in a Spark list in Flex 4?

<s:List id="lst"
labelField="#label"
change="lst_changeHandler(event)"
horizontalCenter="0" verticalCenter="0">
<s:dataProvider>
<s:XMLListCollection>
<fx:XMLList xmlns="">
<node label="One" />
<node label="Two" />
<node label="Three" />
<node label="Four" />
<node label="Five" />
<node label="Six" />
<node label="Seven" />
<node label="Eight" />
<node label="Nine" />
</fx:XMLList>
</s:XMLListCollection>
</s:dataProvider>
</s:List>
protected function lst_changeHandler(event:IndexChangeEvent):void
{
Alert.show(event.target.selectedItem);
}
I want to extend the list with hyperlinks. For example in every node I should have an href attribute also. Then I need to redirect users to the selected item. I know Flash has the URLRequest class.
The little problem I have now is to get the selected item. It propably is because of the dataprovider is xml and I haven't done the correct casting. Or maybe some more enlightened than me can help me.
<fx:Script>
<![CDATA[
import flash.net.navigateToURL;
import spark.events.IndexChangeEvent;
protected function lst_changeHandler(event:IndexChangeEvent):void
{
navigateToURL(new URLRequest(lst.selectedItem.#url));
}
]]>
</fx:Script>
<s:List id="lst"
labelField="#label"
change="lst_changeHandler(event)"
horizontalCenter="0" verticalCenter="0"
>
<s:dataProvider>
<s:XMLListCollection>
<fx:XMLList xmlns="">
<node label="One" url="www.internet.com" />
<node label="Two" url="www.internet2.com" />
<node label="Three" url="www.internet3.com" />
<node label="Four" url="www.bla.com" />
</fx:XMLList>
</s:XMLListCollection>
</s:dataProvider>
</s:List>
var item:XML = event.target.selectedItem as XML;
var label:String = item.#label;
var url:String = item.#url;
if(url != null) {
var ur:URLRequest = new URLRequest(url);
navigateToURL(ur);
}
It was really real simple!

Linear tree in Flex

<mx:Script>
<![CDATA[
private function openAllNodes():void {
tree.openItems = dp..node;
}
private function closeAllNodes():void {
tree.openItems = [];
}
]]>
</mx:Script>
<mx:XML id="dp">
<root>
<node label="Parent 1">
<node label="Child 1" />
<node label="Child 2">
<node label="Grandchild 1" />
<node label="Grandchild 2" />
</node>
<node label="Child 3" />
<node label="Child 4" />
</node>
</root>
</mx:XML>
<mx:ApplicationControlBar dock="true">
<mx:Button label="Open all nodes" click="openAllNodes();" />
<mx:Button label="Close all nodes" click="closeAllNodes();" />
</mx:ApplicationControlBar>
<mx:Tree id="tree"
dataProvider="{dp}"
showRoot="false"
labelField="#label"
width="200" />
Unless or other wise i click my parent list, the child or the next list must be in a disabled state.
I click on Child 1, then only Child 2 Must be able to select.
Please Help Me.
It sounds like you might want to extend the tree class and override some of the methods to implement your special functionality. Look at overriding the drawItem, mouseClickHandler, and possibly the expandItem functions.

Adding a child node to XML in flex

In a Flex application, I'm have an xml object that I'm binding to a tree control. I'm able to add a child node to the xml but when I try to add a child to the child node it doesn't appear on the tree control
tree = <node label="Root">
<node label="Category 1"/>
<node label="Category2"/>
<node label="Category3"/>
<node label="Category 4">
<node label="SubCategory4.1"/>
<node label="SubCategory4.2"/>
</node>
</node>;
var someNode:XMLNode = new XMLNode(9, 'Category5');
var aSubNode:XMLNode = new XMLNode(9, 'SubCategory5.1');
someNode.appendChild(aSubNode);
tree.appendChild(someNode);
So Category5 appears on the tree control but SubCategory5.1 does not. What am I missing?
If you are using flex, use AS3. XMLNode is AS2. In short, try this:
tree = <node label="Root">
<node label="Category 1"/>
<node label="Category2"/>
<node label="Category3"/>
<node label="Category 4">
<node label="SubCategory4.1"/>
<node label="SubCategory4.2"/>
</node>
</node>;
var someNode:XML = <node label="Category5"/>;
var aSubNode:XML = <node label="SubCategory5.1"/>;
someNode.appendChild(aSubNode);
tree.appendChild(someNode);

Resources