0

I am trying to filter an XML based on multiple parameters that I have as an input.

I am trying to identify the parent nodes which have the matching records so that I can filter them out and process.

<A>
    <B1>
        <C1>
            <D1>111</D1>
            <E1>111</E1>
            <F1>
                <G1>111</G1>
                <H1>
                    <I1>111</I1>
                    <J1>111</J1>
                </H1>
            </F1>
        </C1>
    </B1>
    <B1>
        <C1>
            <D1>222</D1>
            <E1>333</E1>
            <F1>
                <G1>222</G1>
                <H1>
                    <I1>222</I1>
                    <J1>222</J1>
                </H1>
            </F1>
        </C1>
    </B1>
    <B1>
        <C1>
            <D1>333</D1>
            <E1>333</E1>
            <F1>
                <G1>333</G1>
                <H1>
                    <I1>333</I1>
                    <J1>333</J1>
                </H1>
            </F1>
        </C1>
    </B1>
</A>

Lets say I need to match for the node D1 and E1 and I1, but if there is a 'AND' match from all the parameters, I need to have the node right from <B1> for the result.

<B1>
    <C1>
        <D1>222</D1>
        <E1>333</E1>
        <F1>
            <G1>222</G1>
            <H1>
                <I1>222</I1>
                <J1>222</J1>
            </H1>
        </F1>
    </C1>
</B1>

I am trying usng the below combination to get the data:

xml..*.((hasOwnProperty("D1") && D1 == "222")&&hasOwnProperty("E1") && D1 == "333"))

But think there is some gap. Can someone fill in and tell me where am I going wrong or is there a better approach to filter an XML? Also, is there something which the filterFunction (collections) can help out with?

1 Answer 1

1

You can use the .. operator, or call its equvalentdescendants() to get an XMLList of all sub nodes that match your criteria, regardless of where they are in the hierarchy. Then, using xml.( criteria ), select only the nodes that also match the expression within the parentheses:

var result:XMLList = xml..B1.(
   ( descendants ("D1") == "222" ) && 
   ( descendants ("E1") == "333" ) && 
   ( descendants ("I1").length() > 0 )  // or any other expression
   );
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to create the Query part dynamically so that params and the keys take over for building the criteria part. Just to show: var q1:String = "descendants (\"E1" ) == \"111\" )";//dynamic query building, this is just for demo. Alert.show("Query>>"+q1.toString()); //Output: descendants ("E1" ) == "111" var r2:XMLList = xmlData..*.(descendants ("E1" ) == "111");//Gives correct answer var r1:XMLList = xmlData..*.(q1.toString());// Gives weird response, doesn't filter out anything. Is something missing here or is this the wrong approach for it.
The expression inside the parentheses should not be a string. But apart from that: You should not ask questions in comments, even as a follow-up - post a new question instead! :)
Ok, thanks for the StackTip:) Followup question posted. stackoverflow.com/questions/9185413/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.