问题
Follow up question on Filtering XML based on multiple params - Flex.
I was trying to create the Query part dynamically so that params and the keys take over for building the criteria part. Just to show:
//building a dynamic query like this
var q1:String = "descendants (\"E1" ) == \"111\" )";
Alert.show("Query>>"+q1.toString()); // Output: descendants ("E1" ) == "111"
var r2:XMLList = xmlData..*.(descendants ("E1" ) == "111"); //returns correct result
var r1:XMLList = xmlData..*.(q1.toString()); // weird response,
// doesn't filter out anything
A comment here Filtering XML based on multiple params - Flex says that the expression inside the parentheses should not be a string. Agreed.
So, is there any other approach to filter out multiple parameters in XML than to use the
descendants(), which currently is not working out because we are building the search query dynamically, based on the params searched?
Any pointers/ideas would be of great help.
回答1:
How about doing the filtering in a loop?
var filters : Array = [ { key : "E1", value : "333" }, { key : "D1", value : "333" } ];
var searchItem : XMLList = xmlData.*;
for each (var filter:Object in filters)
searchItem = searchItem.( descendants ( filter.key ) == filter.value);
trace (searchItem);
来源:https://stackoverflow.com/questions/9185413/dynamic-xml-filtering-based-on-params