How to Iterate nodes in XML response

Mrman
Tera Guru

Hi All,

I'm looking for some help pulling some data from an XML document that is grabbed via a SOAP API .

The structure of the XML document is as follows:

I am trying to get the Monthly salary(MON) and Annual Base salary (ANN) where the <frequency> is mentioned as ANN or MON .

I should iterate through all the <paycompensation_recurring> nodes and based on Frequency and End date I should get the Value of <paycompvalue> which is the amount . 

Please let me know how to iterate all the <paycompensation_recurring> nodes and when my condition matches based on frequency and enddate I wan to get the value.

 

Below is the Source script i Created as part of Transform map and this is only getting value of first instance of <paycompensation_recurring> . It is not going to all nodes.

 

answer = (function getValueForTarget(request, response) {

    var PAY_FREQ_PATH = "//person/employment_information/compensation_information/paycompensation_recurring/frequency";
    var PAY_VALUE_PATH = "//person/employment_information/compensation_information/paycompensation_recurring/paycompvalue";

    var requestData = JSON.parse(request);
    var xmlDoc = new XMLDocument2();
    xmlDoc.parseXML(response);
    var freqID = xmlDoc.getNode(PAY_FREQ_PATH).getTextContent();
    var payvalueID = xmlDoc.getNode(PAY_VALUE_PATH).getTextContent();
    
    if (freqID == 'MON' && payvalueID.toString().trim() != "") {
        
        return payvalueID;
    }
})(request, response);
<person>
                        
                        <employment_information>
                            
                            <compensation_information>
                                                                                             
                                <end_date>9999-12-31</end_date>
                                <event>12</event>
                                                               
                                <paycompensation_recurring>
                                    <annualizationFactor>13.0</annualizationFactor>
                                    
                                    <currency_code>BRL</currency_code>
                                    <end_date>9999-12-31</end_date>
                                    <frequency>M13</frequency>
                                    <is_target>false</is_target>
                                    
                                    <pay_component>PC</pay_component>
                                    <pay_component_type>AMOUNT</pay_component_type>
                                    <paycompvalue>1793.17</paycompvalue>
                                    <seq_number>1</seq_number>
                                    <start_date>2017-11-01</start_date>
                                </paycompensation_recurring>
                                <paycompensation_recurring>
                                    <annualizationFactor>1.0</annualizationFactor>
                                    
                                    <currency_code>BRL</currency_code>
                                    <end_date>9999-12-31</end_date>
                                    <frequency>ANN</frequency>
                                    <is_target>false</is_target>
                                    
                                    <pay_component>PC</pay_component>
                                    <pay_component_type>AMOUNT</pay_component_type>
                                    <paycompvalue>23311.21</paycompvalue>
                                    <seq_number>1</seq_number>
                                    <start_date>2017-11-01</start_date>
                                </paycompensation_recurring>
                            </compensation_information>
                            <compensation_information>
                                
                                <custom_double5>1632.59</custom_double5>
                                <end_date>2017-10-31</end_date>
                                <event>12</event>
                                <event_reason>PAYEQT</event_reason>
                                <is_eligible_for_benefits>false</is_eligible_for_benefits>
                                <is_eligible_for_car>false</is_eligible_for_car>
                                
                                <seq_number>1</seq_number>
                                <start_date>2016-11-01</start_date>
                                <paycompensation_recurring>
                                    <annualizationFactor>13.0</annualizationFactor>
                                    <created_by>09128827</created_by>
                                    
                                    <currency_code>BRL</currency_code>
                                    <end_date>2017-10-31</end_date>
                                    <frequency>M13</frequency>
                                    <is_target>false</is_target>
                                    
                                    <pay_component>PC</pay_component>
                                    <pay_component_type>AMOUNT</pay_component_type>
                                    <paycompvalue>1763.2</paycompvalue>
                                    <seq_number>1</seq_number>
                                    <start_date>2016-11-01</start_date>
                                </paycompensation_recurring>
                                <paycompensation_recurring>
                                    <annualizationFactor>1.0</annualizationFactor>
                                    
                                    <currency_code>BRL</currency_code>
                                    <end_date>2017-10-31</end_date>
                                    <frequency>ANN</frequency>
                                    <is_target>false</is_target>
                                    
                                    <pay_component>PC</pay_component>
                                    <pay_component_type>AMOUNT</pay_component_type>
                                    <paycompvalue>22921.56</paycompvalue>
                                    <seq_number>1</seq_number>
                                    <start_date>2016-11-01</start_date>
                                </paycompensation_recurring>
                            </compensation_information>
                            <compensation_information>
                                
                                <custom_double5>1419.05</custom_double5>
                                <end_date>2016-10-31</end_date>
                                <event>12</event>
                                <event_reason>PAYEQT</event_reason>
                                <is_eligible_for_benefits>false</is_eligible_for_benefits>
                                <is_eligible_for_car>false</is_eligible_for_car>
                                
                                <seq_number>1</seq_number>
                                <start_date>2016-09-01</start_date>
                                <paycompensation_recurring>
                                    <annualizationFactor>13.0</annualizationFactor>
                                                                      
                                    <currency_code>BRL</currency_code>
                                    <end_date>2016-10-31</end_date>
                                    <frequency>M13</frequency>
                                    <is_target>false</is_target>
                                    <last_modified_by>09128827</last_modified_by>
                                    
                                    <pay_component>PC</pay_component>
                                    <pay_component_type>AMOUNT</pay_component_type>
                                    <paycompvalue>1632.59</paycompvalue>
                                    <seq_number>1</seq_number>
                                    <start_date>2016-09-01</start_date>
                                </paycompensation_recurring>
                                <paycompensation_recurring>
                                    <annualizationFactor>1.0</annualizationFactor>
                                  
                                    <currency_code>BRL</currency_code>
                                    <end_date>2016-10-31</end_date>
                                    <frequency>ANN</frequency>
                                    <is_target>false</is_target>
                                    
                                    <pay_component>PC</pay_component>
                                    <pay_component_type>AMOUNT</pay_component_type>
                                    <paycompvalue>21223.67</paycompvalue>
                                    <seq_number>1</seq_number>
                                    <start_date>2016-09-01</start_date>
                                </paycompensation_recurring>
                            </compensation_information>
</employment_information>
                    </person>
1 ACCEPTED SOLUTION

Hi @ramamr 

a working example looks like this:

var requestData = JSON.parse(request);
var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(response);

var paycompensationRecurringNode = xmlDoc.getFirstNode("//paycompensation_recurring");

while(paycompensationRecurringNode != null) {
  var nodeValues    = {};
  var childIterator = paycompensationRecurringNode.getChildNodeIterator();
    
  while (childIterator.hasNext()) {
    var childNode      = childIterator.next();
    var childNodeName  = childNode.getNodeName();
    var childNodeValue = childNode.getTextContent();
    
    nodeValues[childNodeName] = childNodeValue;
  }
  
  
  gs.info(nodeValues.pay_component);
  gs.info(nodeValues.annualizationFactor);
  
  paycompensationRecurringNode = xmlDoc.getNextNode(paycompensationRecurringNode);
}

 

At object nodeValues you will find the values of the child nodes of each paycompensation_recurring node.

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

View solution in original post

7 REPLIES 7

Hi @ramamr 

Did my reply answer your question?

If so, please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

If not, please tell me what you are still missing!

Kind regards
Maik

Hello Maik,

 

Above Iterator Works find for the Item and first level Childs, however it doesn't work for the sub child levels ..how do get this work for the tagSet/Item and Fetch the key and values of the same item..as getNextNode, would get the next item.. not the tagSet/item[2]

In the belowCase ( one case example) ... I have Number of SubnetSet/item
can your code be modified to fetch the tagSet / item aka key and values of that item only?? also

  <subnetSet>

        <item>
            <subnetId>subnet-015ec55c4e33b37b9</subnetId>
            <subnetArn>arn:aws:ec2:us-east-1:320055800654:subnet/subnet-015ec55c4e33b37b9</subnetArn>
            <state>available</state>
            <ownerId>320055800654</ownerId>
            <vpcId>vpc-0dc8d6d31bac6cfc0</vpcId>
            <cidrBlock>10.0.0.128/25</cidrBlock>
            <ipv6CidrBlockAssociationSet/>
            <availableIpAddressCount>123</availableIpAddressCount>
            <availabilityZone>us-east-1e</availabilityZone>
            <availabilityZoneId>use1-az3</availabilityZoneId>
            <defaultForAz>false</defaultForAz>
            <mapPublicIpOnLaunch>false</mapPublicIpOnLaunch>
            <tagSet>
                <item>
                    <key>Name</key>
                    <value>my-subnet-test02</value>
                </item>
                <item>
                    <key>Props</key>
                    <value>Kyndryl</value>
                </item>
            </tagSet>
            <assignIpv6AddressOnCreation>false</assignIpv6AddressOnCreation>
            <mapCustomerOwnedIpOnLaunch>false</mapCustomerOwnedIpOnLaunch>
            <privateDnsNameOptionsOnLaunch>
                <hostnameType>ip-name</hostnameType>
                <enableResourceNameDnsARecord>false</enableResourceNameDnsARecord>
                <enableResourceNameDnsAAAARecord>false</enableResourceNameDnsAAAARecord>
            </privateDnsNameOptionsOnLaunch>
            <ipv6Native>false</ipv6Native>
            <enableDns64>false</enableDns64>
        </item>
        <item>
            <subnetId>subnet-00389669d765ef621</subnetId>
            <subnetArn>arn:aws:ec2:us-east-1:320055800654:subnet/subnet-00389669d765ef621</subnetArn>
            <state>available</state>
            <ownerId>320055800654</ownerId>
            <vpcId>vpc-0dc8d6d31bac6cfc0</vpcId>
            <cidrBlock>10.0.0.0/25</cidrBlock>
            <ipv6CidrBlockAssociationSet/>
            <availableIpAddressCount>123</availableIpAddressCount>
            <availabilityZone>us-east-1e</availabilityZone>
            <availabilityZoneId>use1-az3</availabilityZoneId>
            <defaultForAz>false</defaultForAz>
            <mapPublicIpOnLaunch>false</mapPublicIpOnLaunch>
            <tagSet>
                <item>
                    <key>Name</key>
                    <value>my-subnet-test01</value>
                </item>
            </tagSet>
            <assignIpv6AddressOnCreation>false</assignIpv6AddressOnCreation>
            <mapCustomerOwnedIpOnLaunch>false</mapCustomerOwnedIpOnLaunch>
            <privateDnsNameOptionsOnLaunch>
                <hostnameType>ip-name</hostnameType>
                <enableResourceNameDnsARecord>false</enableResourceNameDnsARecord>
                <enableResourceNameDnsAAAARecord>false</enableResourceNameDnsAAAARecord>
            </privateDnsNameOptionsOnLaunch>
            <ipv6Native>false</ipv6Native>
            <enableDns64>false</enableDns64>
        </item>
    </subnetSet>

Slava Savitsky
Giga Sage

To go over multiple nodes, you will need to use getFirstNode() and getNextNode() methods instead of getNode() you are currently using. Here is an example from the documentation.