简体   繁体   中英

In Groovy, how do I get the value in an element with a specific attribute in XML

I have a groovy script that compares an XML response to a JDBC query. I am trying to account for a scenario where I need to validate a XML value based on a specific attribute in the element.

Here is an example of the XML:

               <FormInstance id="Impairment_0111235228_2_174_477">
              <FormName>NEW MEDICAL HISTORY NOTE</FormName>
              <ProviderFormNumber>174</ProviderFormNumber>
              <FormResponse id="Imapairment_477_6">
                 <QuestionNumber>477</QuestionNumber>
                 <QuestionText>ExamOne Rx</QuestionText>
                 <AnswerChoice>
                    <AnswerChoiceText>ExamOne Result</AnswerChoiceText>
                    <OLifEExtension VendorCode="08" ExtensionCode="ImpairmentAttributeType">LBL</OLifEExtension>
                    <OLifEExtension VendorCode="08" ExtensionCode="ImpairmentAttributeSeq">2</OLifEExtension>
                 </AnswerChoice>
                 <OLifEExtension VendorCode="08" ExtensionCode="ImpairmentSequence">1</OLifEExtension>
                 <OLifEExtension VendorCode="08" ExtensionCode="QuestionSequence">6</OLifEExtension>
              </FormResponse>
           </FormInstance>

As you can see, there are several elements with the name "OLifEExtension", but with unique ExtensionCode attributes. I need to be able to compare the value by attribute. For example, using the XML above, if the attribute ExtensionCode = QuestionSequence, I expect to see a value of 6.

I have tried the following in my script:

    obj.questionSequence = xml.FormResponse.OLifEExtension.{'@ExtensionCode' == 'QuestionSequence'}

    obj.questionSequence = xml.FormResponse.{it.name() == 'OLifEExtension' && it.@ExtensionCode == 'QuestionSequence'}

    obj.questionSequence = xml.FormResponse.OLifEExtension.{it.@ExtensionCode == 'QuestionSequence'}

The syntax on all 3 examples appear to be correct because I don't get any errors when I run the script, but when I output results, it is always empty.

I also tried:

obj.questionSequence = xml.FormResponse.OLifEExtension[1]

This works, but the problem here is that QuestionSequence may not always exist in the same spot. It could be position 0 or 2. So, I really need it to work off the attribute name.

Also, the base of the script that I am using is in the response to this question if that is needed by anyone.

Compare JDBC Response to an XML Response where number of nodes vary and order could change?

Thank you in advance for any help.

Of course, right after I ask the question, I figure it out. If anyone has a cleaner way to do it, I would appreciate it, but this seems to do the trick.

obj.questionSequence = xml.FormResponse.OLifEExtension.find{it.@ExtensionCode == 'QuestionSequence'}     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM