0

I want to fetch the element amount and its value from the below XML using XPath expression.

<donor>
    <beneficiaries>
        <beneficiary>
            <balance>10</balance>
            <additionaData>
                <name>age</name>
                <value>40</value>
            </additionaData>
            <additionaData>
                <name>amount</name>
                <value>1000</value>
            </additionaData>
        </beneficiary>
        <beneficiary>
            <balance>10</balance>
            <additionaData>
                <name>age</name>
                <value>50</value>
            </additionaData>
            <additionaData>
                <name>amount</name>
                <value>4000</value>
            </additionaData>
        </beneficiary>
    </beneficiaries>
</donor>

I am able to get the NodeList for beneficiary element but not able to get the content of tag name and value.

Update:

I want to first check if the tag <name> in <additionalData> is amount if yes fetch the content of <value> for all <beneficiary>.

2 Answers 2

1

This XPath,

/donor/beneficiaries/beneficiary/additionaData[name='amount']/value

will select the two value elements associated with "amount" name elements:

<value>1000</value>
<value>4000</value>

This XPath,

/donor/beneficiaries/beneficiary[additionaData[name='age']/value = 50]
                    /additionaData[name='amount']/value

will select only the "amount" value element for the beneficiary of age 50:

<value>4000</value>

Update:

i actually want to first check if the tag <name> in <additionalData> is amount if yes fetch the content of for all <beneficiary>

This XPath,

/donor/beneficiaries/beneficiary[additionaData/name='amount']

will select all beneficiary elements whose additionaData/name equals "amount":

<beneficiary> 
  <balance>10</balance>  
  <additionaData> 
    <name>age</name>  
    <value>40</value> 
  </additionaData>  
  <additionaData> 
    <name>amount</name>  
    <value>1000</value> 
  </additionaData> 
</beneficiary>

<beneficiary> 
  <balance>10</balance>  
  <additionaData> 
    <name>age</name>  
    <value>50</value> 
  </additionaData>  
  <additionaData> 
    <name>amount</name>  
    <value>4000</value> 
  </additionaData> 
</beneficiary>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks .. i actually want to first check if the tag <name> in <additionalData> is amount if yes fetch the content of <value> for all <beneficiary>
thanks again .. i have never used Xpath before . Now how do i iterate over the NodeList to get the content of <value> where <name> is amount.
You're welcome. Is your latest request not satisfied by the original XPath I offered? I've already given you several variations. Please accept this answer if it's helped, and ask a new question rather than continuously extending this one. Be sure to be clear to state what you want to select in specific declarative, not procedural, terms. Thanks.
Sorry for the confusion. :-) .. Yes your answer is useful !!! After using the expression (/donor/beneficiaries/beneficiary[additionaData/name='amount']) now i have the NodeList. My requirement is to iterate all the beneficiary node and check the <additionalData> if the <name> is amount fetch the content of <value>
What you're failing to recognize is that XPath can take you all the way to the node or nodes you want without additional iteration. I cannot help you further because you refuse to simply state what you wish to select in the end. Good luck.
0

The below xpath will give values

//donor/beneficiaries/beneficiary/additionaData/*[name()='name'][.='amount']/following-sibling::*[name()='value']

Element='<value>1000</value>'
Element='<value>4000</value>'

If you want to get age nodes

modify the above xpath by replacing amount with age

3 Comments

thanks .. i have edit the question i need to fetch the content of <value> if the <name> is amount for all beneficiary,
@Pankaj do you want a java implementation to iterate over nodelist of <name> elements?
hi svasa .. My requirement is to iterate all the beneficiary node and check the <additionalData> if the <name> is amount fetch the content of <value>

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.