PDA

View Full Version : xml filtering


Qutaibah
08-05-2007, 04:22 PM
Hello all,

I have run into a difficulty xml. I am hopping for some help, any help would be appreciated. here is the situation:

I am trying to filter xml item based on its child elements. for instance:

<items>
<item name="box1">
<content cCode="04">Substanc 04</content>
<weight unit="lb">2000</weight>
<location sCode="02">StG-02</location >
</item>
<item name="box2">
<content cCode="04">Substanc 04</content>
<weight unit="lb">1200</weight>
<location sCode="02">StG-02</location >
</item>
<item name="box3">
<content cCode="24">Substanc 24</content>
<weight unit="lb">1400</weight>
<location sCode="02">StG-02</location >
</item>
<item name="box4">
<content cCode="04">Substanc 04</content>
<weight unit="lb">5000</weight>
<location sCode="02">StG-02</location >
</item>
</items>

a few months ago, Dr_Zeus helped me with a snippet of code that can help filter the xml document. The code provided looks like this:

var myFilter =
[{name: "content", value: "Substanc 04"},
{name: "weight", value: "4000"}];

// LOOP
var filterName = myFilter[i].name;
var filterValue = myFilter[i].value;
var myList = myList.(child(filterName) == filterValue );


with the code as it is, I will only be able to select "substance 04" items whose weight is == to 4000. in this case (the case of the xml document provided above) the are no matches

What I am trying to do, is select all "substance 04" items whose weight is EQUAL or LESS than 4000.

I really to tired hard to resolve this issue but couldn't figure it out. Any help would be appreciated.

Colin Campbell
08-05-2007, 06:45 PM
I'm not quite certain how to implement it into your filtering system there, but here is how it would be accomplished without the system:


var myList = myList.(child("content") == "Substance 04" && child("weight") <= "4000" );

Jim Freer
08-05-2007, 11:55 PM
Thanks, Dr_Zeus!

That's a good function to have around, so I wrote (and tested) in my own style. Sorry, Dr_Zeus & Qutaibah, it's a bad habit of mine.

private function testXmlListFilter
()
:void
{
var lvXml:XML =
<items>
<item name="box1">
<content cCode="04">Substanc 04</content>
<weight unit="lb">2000</weight>
<location sCode="02">StG-02</location >
</item>
<item name="box2">
<content cCode="04">Substanc 04</content>
<weight unit="lb">1200</weight>
<location sCode="02">StG-02</location >
</item>
<item name="box3">
<content cCode="24">Substanc 24</content>
<weight unit="lb">1400</weight>
<location sCode="02">StG-02</location >
</item>
<item name="box4">
<content cCode="04">Substanc 04</content>
<weight unit="lb">5000</weight>
<location sCode="02">StG-02</location >
</item>
</items>;

var lvXmlList:XMLList = new XMLList( lvXml.item );

var lvFilter:Object =
[
{name: "content", value: "Substanc 04"},
{name: "weight", value: "4000", comparison: "<=" }
];

lvXmlList = XmlListFilter( lvXmlList, lvFilter );

TextArea1.text = lvXmlList.toXMLString();

} // testXmlListFilter
private function XmlListFilter
( avXmlList :XMLList,
avFilter :Object )
:XMLList
{
var lvXmlList:XMLList = avXmlList.copy();

for( var lvIndex:int = 0; lvIndex < avFilter.length; lvIndex++ )
{
var lvFilterName:String = avFilter[lvIndex].name;
var lvFilterValue:String = avFilter[lvIndex].value;

switch( avFilter[lvIndex].comparison )
{
case ">":
lvXmlList = lvXmlList.( child(lvFilterName) > lvFilterValue );
break;

case ">=":
lvXmlList = lvXmlList.( child(lvFilterName) >= lvFilterValue );
break;

case "!=":
lvXmlList = lvXmlList.( child(lvFilterName) != lvFilterValue );
break;

case "<":
lvXmlList = lvXmlList.( child(lvFilterName) < lvFilterValue );
break;

case "<=":
lvXmlList = lvXmlList.( child(lvFilterName) <= lvFilterValue );
break;

default:
lvXmlList = lvXmlList.( child(lvFilterName) == lvFilterValue );

} // switch

} // for

return lvXmlList;

} // XmlListFilter

http://freerpad.blogspot.com/

Qutaibah
08-06-2007, 01:10 AM
thanks Jim Freer for sharing your approach. neat. I am glad you guys are posting feedback. It absolutely helpful. I came up with another solution my self, which I'll try to post soon.