Read XML with Nested Loops

Read XML with Nested Loops
5 (100%) 2 votes

In this post, I will describe you, how to Read XML having nested loops in Talend. For that I am using below XML as source.

Source XML has list of Items from a retail store,  and <item> node repeated for each item. Inside <item> node we have nested nodes for <batter> and <topping> our task is to read all the items with nested loops in their separate flow.

Image of source XML.

Sample XMl file with Nested Loops

Sample XMl file with Nested Loops

First of all you should create metadata of your XML file, but in this post I am using XSD to populate source schema in tXMLMap.

Create new job and add tFileinputXML component and configured it as shown in Image.

Image of tFileInputXMl component.

We have set XPATH Loop to “/items” as this is the root node of XML file. then I have created one column with “Document” data type. see in Image.

Set tFileInputXML for Nested loops
Set tFileInputXML for Nested loops

tXMLMap and connect with tFileinputXML component using “Main” link.

Open tXMLMap right click on “items” node at right(source) side then select “Import from file”.

provide XSD file it will automatically create all sub nodes. like below image.

Set batter & toppiq as “loop” element by right click  and select ” As loop element”

Configure tXMlMap
Configure tXMlMap

Create two outputs,  “batter” and “tappinq” then drag respective columns to “batter & tappinq”

Click on “set loop function” box it will open a window wherein you have add new row and select respective loop path e.g. for “toppinq” i will select “toppinq” loop like wise batter for “batter” output.

Your Final settings looks like below image.

Configuration tXMLMap output with loop path
Configuration tXMLMap output with loop path

we are ready to get the result add tLogRow components for each  of the output and execute the job it show results shown in image.

Nested XML loop parse Output
Nested XML loop parse Output

 

About Umesh

I am Software consultant with approx 7 years of experience mainly in Business Intelligence and data warehousing assignments using Talend. Writing is not my passion but i am doing it to help others. if you have any special case where you want me to demonstrate then please post me.

3 comments on “Read XML with Nested Loops

    1. You need to design your job like below flow. your first flow is in first input file and second flow is in second input file. use inner join as joining model then you will get all the matching rows from both the flows.
      tmap join

  1. Hi Team,

    My XMl is like below can any one let me know how we can convert it to txt format.. Thanks in Advance

    2017-11-15T05:12:09.987Z
    storefront
    75031785
    USD
    default
    net
    00062384

    00040242
    TAoMY lZiGg
    belktestemailautomation@gmail.com

    john
    joseph
    3850 E Independence Blvd
    Charlotte
    28205
    NC
    US
    333-444-5555

    belktestemailautomation@gmail.com

    NEW
    NOT_SHIPPED
    CONFIRMED
    NOT_PAID

    75031785

    50.00
    0.00
    50.00
    25.00
    Congratulations Gift Card
    50.00
    1
    0499999900673
    Congratulations Gift Card
    2.0
    0.0
    00069710
    false

    50.0
    true
    https://www.belkdev.com/p/belk-congratulations-gift-card/0499999900673.html
    25.0
    0499999900673
    Belk
    9091
    50.0
    true
    0.0
    0.0
    0.0
    0.0
    0.0
    0.0
    0.0
    0.0
    0.0
    0.0
    false
    1
    74.95
    0100001
    25.0
    https://belk.scene7.com/is/image/Belk?layer=0&src=0100001_5490CONGRATS2012_A_002_T10L00&layer=comp&amp;
    Belk gift cards may be redeemed for merchandise & services at any Belk store or at belk.com. Belk gift cards never expire or depreciate in value. Belk gift cards cannot be exchanged for cash, or used to pay your Belk Rewards Card balance.
    0.0
    24.95
    0.0
    0.0
    MECKLENBURG
    0.0
    0.0
    NORTH CAROLINA
    0.0
    0.0
    MECKLENBURG
    0.0
    0.0
    NORTH CAROLINA
    25.0
    25black
    10008197
    No Size

    24.95
    0.00
    24.95
    24.95
    Shipping
    24.95
    STANDARD_SHIPPING
    00069710
    0.0825

    NOT_SHIPPED

    2DAY

    qafirst
    qalast
    3850 E Independence Blvd
    Charlotte
    28205-7305
    NC
    US
    333-444-5555

    failed
    QAAddressName
    true

    false

    50.00
    0.00
    50.00

    50.00
    0.00
    50.00

    24.95
    0.00
    24.95

    24.95
    0.00
    24.95

    74.95
    0.00
    74.95

    failed
    Cqaaddressname

    50.00
    0.00
    50.00

    50.00
    0.00
    50.00

    24.95
    0.00
    24.95

    24.95
    0.00
    24.95

    74.95
    0.00
    74.95

    DISCOVER
    XXXX-XXXX-XXXX-9424
    Bruce Wayne
    6011002287499424
    7
    2019

    8a7aae88ba0eb4f2adc5ef5f00c2d76807efc4a6@2017-11-14 15:06:03 +0000
    6011002287499424
    2017-11-15 05:11:44+0000
    1
    8a829418576a558101576b0c0116023b_2c740cc61e87492d99f505fe74ab46b3

    74.95
    ACI_BASIC_CREDIT
    75031785

    8a7aae88ba0eb4f2adc5ef5f00c2d76807efc4a6@2017-11-14 15:06:03 +0000
    000748
    2017-11-22 05:12:09+0000
    2017-11-15 05:12:09+0000
    1
    D888319942427720
    8a829418576a558101576b0c0116023b_4339d016c25f4167a0e166fef0390384
    8a8294495fa65eba015fbe1772c75223
    00
    Approved
    1403406640

    66.85.48.2

    0888
    ACCEPT
    false
    true
    ECDW
    STH
    MerchandiseAmount;50.00;CouponDiscount;0.00;AssociateDiscount;0.00;ShippingSurcharges;0.00;RewardDollars;0.00;ShippingAmount;24.95;TaxesAmount;0.00;OrderAmount;74.95;GiftBoxAmt;0.00;BelkGiftCardAmount;0.00
    false
    1

Leave a Reply

Your email address will not be published. Required fields are marked *