PDA

View Full Version : XMLList & searching tree control . . .


webgyver
04-12-2008, 07:31 PM
Yes, I'm somewhat of a newbie with Flex 3 & ActionScript, but I have been trying to read up as much as possible about this issue before asking for help.

Basically, I need to a) read data into a mx control from an external XML file (changes daily) and b) allow the end user to search for specific items in that control.

So, in my prototype I've used a couple of tree controls. Tree control 1 uses the following data provider:

<mx:XML id="mlb" source="http://www.myserver.com/ndncr/small.xml" />

<mx:Tree id="tree" showRoot="false" dataProvider="{mlb}" labelField="@val" width="186" x="224" y="86" height="64"/>


So far, so good. I can even search through the contents of that tree control, find the node with the corresponding value, and highlight it for the user.

Here's the basic structure of the external XML file (this is how I receive the file from a national data provider):

<list type='full' level='state' val='UT'>
<ac val='435'>
<ph val='2000071' />
<ph val='2000072' />
<ph val='2000074' />
<ph val='2000075' />
<ph val='2000248' />
<ph val='2000280' />
<ph val='2001031' />
<ph val='2001032' />
<ph val='2002029' />
<ph val='2004309' />
<ph val='2006900' />
<ph val='9949900' />
<ph val='9949999' />
</ac>
<ac val='801'>
<ph val='2010000' />
<ph val='2010003' />
<ph val='2010004' />
<ph val='2010010' />
<ph val='2010011' />
<ph val='2010012' />
<ph val='2010014' />
<ph val='9998900' />
<ph val='9999999' />
</ac>
</list>


The problem is, this is just a prototype, and the external XML file is just a short, short version of the original 25MB file that I have to download (and read into Flex) every day.

So, for my tree control 2, I've tried to use a more sophisticated (?) approach, but I've gotten myself into a bit of trouble. Since about 8 p.m. Friday night, I have tried just about every imaginable syntax combination to make this work, but I must be doing something very wrong. Or perhaps misunderstand a very basic concept.



The thing that's not working: I cannot figure out how to search for a specific value in tree control 2.

The problem lies with:

var node:XMLList = xmlListPN.ac.ph.(@val == searchStr);

And this is just one of the many iterations I've tried.

Would anyone please point me in the right direction?



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
verticalAlign="top"
backgroundColor="white"
themeColor="haloGreen" >


<mx:Script>
<![CDATA[
import mx.rpc.events.*;
import mx.utils.ObjectProxy;
import mx.controls.Alert;
import mx.events.ListEvent;

[Bindable]
private var searchResult:XMLList;
private var searchResultIndex:uint = 0;
private var alert:Alert;
public var total:uint = 0;

[Bindable]
public var xmlListAC:XMLList;
[Bindable]
public var xmlListPN:XMLList;


// SEARCH 801
private function comboBox_change(evt:MouseEvent):void {

var searchStr:String = phoneNumber.text;

if(searchStr.length == 0){
Alert.show("Please, enter a phone number before searching.");
return;
}

var node:XMLList = mlb.ac.ph.(@val == searchStr);

expandParents(node[0]);
tree.selectedItem = node[0];
var idx:int = tree.getItemIndex(node[0]);
tree.scrollToIndex(idx);
}


private function expandParents(node:XML):void {
if (node && !tree.isItemOpen(node)) {
tree.expandItem(node, true);
expandParents(node.parent());
}
}



// SEARCH 435
private function search435(evt:MouseEvent):void {

var searchStr:String = phoneNumber.text;

if(searchStr.length == 0){
Alert.show("Please, enter a phone number before searching.");
return;
}


var node:XMLList = xmlListPN.ac.ph.(@val == searchStr);

expandParents435(node[0]);
tree435.selectedItem = node[0];
var idx:int = tree435.getItemIndex(node[0]);
tree435.scrollToIndex(idx);
}


private function expandParents435(node:XML):void {
if (node && !tree435.isItemOpen(node)) {
tree435.expandItem(node, true);
expandParents(node.parent());
}
}



private function loadExtXML(src:String):void {
httpService.url = src;
httpService.send();
}

private function httpService_fault(evt:FaultEvent):void {
var title:String = evt.type + " (" + evt.fault.faultCode + ")";
var text:String = evt.fault.faultString;
alert = Alert.show(text, title);
//xmlListAC.removeAll();
//xmlListPN.removeAll();
}


private function httpService_result(evt:ResultEvent):void {

// THIS WORKS:
xmlListAC = XML(evt.result).ac.@val;

// THIS WORKS, TOO:
// var xmlListPN:XMLList = XML(evt.result).ac.ph.@val;

// THIS IS WHAT I WANT:
// xmlListPN = XML(evt.result).ac;

// trace(xmlListPN.toXMLString());

//total = 0;
//for each (var prop:XML in xmlListPN) {
// total += 1;
//trace(prop.@val);
//}

// trace(total);

// Alert.show(xmlList);

/*
if(XML(evt.result).list.@val is ObjectProxy is ObjectProxy){
// only contains one item...not enough for an array
xmlListColl = XML(evt.result).dataroot.list.@val;
}else{
xmlListColl = new XMLListCollection(xmlList);
}
*/


// THIS WORKS, TOO:
var searchStr:String = "435";
var xmlListPN:XMLList = XML(evt.result).ac.(@val == searchStr);

trace(xmlListPN.toXMLString());

tree435.dataProvider = xmlListPN;

//xmlListPN = new XMLList(xmlListPN);
//xmlListAC = new XMLList(xmlListAC);


}


]]>
</mx:Script>


<mx:XML id="mlb" source="http://www.myserver.com/ndncr/small.xml" />


<mx:HTTPService id="httpService"
resultFormat="e4x"
fault="httpService_fault(event);"
result="httpService_result(event)" />


<mx:ApplicationControlBar dock="true" x="0" y="0" width="100%">
<mx:Label text="Do Not Call Registry: Phone Number Lookup" fontWeight="bold" fontSize="18" color="#456B73"/>
</mx:ApplicationControlBar>

<mx:Tree id="tree" showRoot="false" dataProvider="{mlb}" labelField="@val" width="186" x="224" y="86" height="64"/>
<mx:Tree id="tree435" showRoot="false" dataProvider="{xmlListPN}" labelField="@val" x="224" y="170" width="186" height="59"/>

<mx:TextInput id="phoneNumber" x="224" y="47" width="186" textAlign="center" fontSize="12" fontWeight="bold" color="#8A0E60"/>
<mx:Button label="Search 435" click="search435(event)" x="127" y="171"/>
<mx:Button label="Search 801" click="comboBox_change(event)" x="127" y="87"/>


<mx:Button label="Load Small File" click="loadExtXML('http://www.myserver.com/ndncr/small.xml');" x="300" y="237"/>
<mx:Button label="Load Big File" click="loadExtXML('http://www.myserver.com/ndncr/2008-4-10_UT_2E17C25E-0025-4AAE-AD8C-7668B25A9D50.xml');" x="313" y="495"/>
<mx:Label x="50" y="47" text="Enter the Phone Number:" fontSize="12" fontWeight="bold"/>


</mx:Application>