Question
prashanth ponugoti · Nov 15

for loop to loop xml repetitive tag content

Hi Friends ,

Here I need fetch the  OrgCode  value from party tag  whose  AgentId=1

<Msg>

<Parties>

<Party AgentId=0>

<OrgCode>abcd</OrgCode>

</Party>

 

<Party AgentId=1>

<OrgCode>FCA</OrgCode>

</Party>

 

<Party AgentId=2>

<OrgCode>FCA</OrgCode>

</Party>

 

</Msg>

</Parties>

Here is my loop logic which is not working , could you please help me

FOR party=MyRequest.Msg.Parties.Party {
      if party.AgentId ="1"
      {
      set recipientCode=party.OrgCode 
      }
    }

 

Thank you

Product version: IRIS 2022.1
0
0 120
Discussion (2)1
Log in or sign up to continue

This might be easier to do with XPath rather than a loop.

First, you'll need to create a %XML.XPATH.Document object, maybe using the create from string method:

set sc = ##class(%XML.XPATH.Document).CreateFromString(xmlstring,.mydoc)

Check that the status you get back from that is not an error. If it isn't, you mydoc should be an XPath document. Then you should be able to use the EvaluateExpression method of that document to get what you want, something like:

set sc = mydoc.EvaluateExpression("/Msg/Parties/Party[AgentId=1]/OrgCode","",.value)

If that status is okay, the value you're looking for will be in value, unless there are multiple XML nodes that match that path. W3 provides the XPath syntax specification here.