Looping through XML file on basis of a node


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping through XML file on basis of a node
# 1  
Old 05-12-2015
Looping through XML file on basis of a node

Code:
<?xml version="1.0" encoding="UTF-8"?>

<Document>
  <FIToFICstmrCdtTrf>
    <GrpHdr>
      <MsgId>10001</MsgId>
      <NbOfTxs>1</NbOfTxs>
      <IntrBkSttlmDt>2015-05-06</IntrBkSttlmDt>
      <SttlmInf>
        <SttlmMtd>CLRG</SttlmMtd>
      </SttlmInf>
      <PmtTpInf>
        <CtgyPurp>
          <Cd>CIOO</Cd>
        </CtgyPurp>
      </PmtTpInf>
      <InstgAgt>
        <FinInstnId>
          <BICFI>JOHNCENA</BICFI>
        </FinInstnId>
        <BrnchId>
          <Id>0313</Id>
        </BrnchId>
      </InstgAgt>
      <SplmtryData>
        <Envlp>
          <Cnts/>
        </Envlp>
      </SplmtryData>
    </GrpHdr>
    <CdtTrfTxInf> 
      <InstdAgt>
        <FinInstnId>
          <BICFI>JOSH</BICFI>
        </FinInstnId>
      </InstdAgt>
      <Dbtr>
        <Nm>XXXXXXXX XX XXXXXXX</Nm>
        <PstlAdr>
          <AdrLine>XXXXXXXX XX XXXXXXX</AdrLine>
        </PstlAdr>
        <Id>
          <PrvtId>
            <Othr>
              <Id/>
            </Othr>
          </PrvtId>
          <OrgId>
            <Othr>
              <Id/>
            </Othr>
          </OrgId>
        </Id>
      </Dbtr>
      <DbtrAcct>
        <Id>
          <Othr>
            <Id>0000001065</Id>
          </Othr>
        </Id>
      </DbtrAcct>
      <DbtrAgt>
        <FinInstnId>
          <BICFI>JOHNCENA</BICFI>
        </FinInstnId>
        <BrnchId>
          <Id>0313</Id>
        </BrnchId>
      </DbtrAgt>
      <CdtrAgt>
        <FinInstnId>
          <BICFI>KHALI</BICFI>
        </FinInstnId>
        <BrnchId>
          <Id/>
        </BrnchId>
      </CdtrAgt>
      <Cdtr>
        <Nm>TEST</Nm>
        <PstlAdr>
          <AdrLine/>
        </PstlAdr>
        <Id>
          <PrvtId>
            <Othr>
              <Id/>
            </Othr>
          </PrvtId>
          <OrgId>
            <Othr>
              <Id/>
            </Othr>
          </OrgId>
        </Id>
      </Cdtr>
      <CdtrAcct>
        <Id>
          <Othr>
            <Id>12434345</Id>
          </Othr>
        </Id>
      </CdtrAcct>
      <InstrForCdtrAgt>
        <InstrInf>DETAILS 1</InstrInf>
      </InstrForCdtrAgt>
      <SplmtryData>
        <Envlp>
          <Cnts>
            <Dbtr>
              <CustTp>3</CustTp>
              <CustRsdt>1</CustRsdt>
            </Dbtr>
            <Cdtr>
              <CustTp>3</CustTp>
              <CustRsdt>1</CustRsdt>
            </Cdtr>
            <PrdCnfrm/>
          </Cnts>
        </Envlp>
      </SplmtryData>
    </CdtTrfTxInf>
  </FIToFICstmrCdtTrf>
</Document>



I have this file wherein at line 109 , there is a tag <Dbtr> followed by tag <CustTp> at line 110 , I want to check if the tag value for <CustTp> is 1 then have only <PrvtId> section from line 43 to 47 inside <Dbtr> tag section from line 37 to 54 and delete <OrgId> section from line 48 to 52.

if the tag value for <CustTp> is 2 or 3 then have only <OrgId> section from line 48 to 52 and delete <PrvtId> section from line 43 to 47 inside <Dbtr> tag section from line 37 to 54 .

Same logic I need to apply for <CustTp> tag inside <Cdtr> tag section on line 113 , in that I need to do the changes in the <Cdtr> section from line 78-95 on basis of value inside <CustTp> tag.

Kindly note that <CdtTrfTxInf> is repeating element , so we can have n number of <CdtTrfTxInf> inside an XML file.


Regards,
Harish
# 2  
Old 05-12-2015
Welcome to the forum. You need an xml parsing tool. more precisely xpath handling. You can google for it.
# 3  
Old 05-12-2015
It may also be possible with awk (or other methods) to manage this file. You will really have to think and KNOW the layout and all the potential possibilities before beginning. Also, be aware that "will something always be at line 109"? In my experience with files, that may not be the case.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Mount FIle systems from node-1 onto node-2

Hi, We have HP UX service guard cluster on OS 11.23. Recently 40+ LUNs presented to both nodes by SAN team but I was asked to mount them on only one node. I created required VGs/LVs, created VxFS and mounted all of them and they are working fine. Now client requested those FS on 2nd node as... (4 Replies)
Discussion started by: prvnrk
4 Replies

2. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

3. Homework & Coursework Questions

Accessing one UNIX node from another node of the same server

Hi Experts, I am in need of running a script from one node say node 1 via node 2. My scheduling tool dont have access to node2 , so i need to invoke the list file from node1 but the script needs to run from node2. because the server to which i am hitting, is having access only for the node... (5 Replies)
Discussion started by: arun1377
5 Replies

4. UNIX for Dummies Questions & Answers

Iterate/Loop Through XML Node List

I need to load an XML file and loop through a list of nodes in it to execute a shell script for each one using the attributes for each node as parameters for the script. Any ideas? Any help will be much appreciated. (1 Reply)
Discussion started by: bradlecat
1 Replies

5. Shell Programming and Scripting

Separate a file on the basis of timestamp

hi, I have a log file which contains data for last 7 seven days. I want to copy all the data for 6 days in one file and keep only the data for today in that log file and mail that log file containing today's data. Log file is in this format Time: 120529 10:51:08 User@Host: local @ ... (4 Replies)
Discussion started by: arijitsaha
4 Replies

6. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

7. Shell Programming and Scripting

How to get the main node name in xml using perl?

Hi, I am having an conf file like this: <Main> <NODE> <NODENAME>FRUITS</NODENAME> <NAME>APPLE</NAME> <COLOUR>RED</COLOUR> <NODE> <IsWrapper/> <NODENAME>SEASONAL</NODENAME> <NODE> <NAME>MANGO</NAME> <COLOUR>GREEN</COLOUR> </NODE> </NODE>... (4 Replies)
Discussion started by: vanitham
4 Replies

8. Shell Programming and Scripting

Split one file to Multiple file with report basis in unix

Hi, Please help on this. i want split the below file(11020111.CLT) to more files with some condition. :b: 1) %s stating of the report 2) %e ending of the report example starting of the report: %sAEGONCA| |MUMBAI | :EXPC|N|D ending of the report %eAEGONCA| |MUMBAI | :EXPC 3)so the... (10 Replies)
Discussion started by: krbala1985
10 Replies

9. Shell Programming and Scripting

How to get value from xml node using sed/perl/script?

hello, new to this forum. but i have a requirement to extract the value from multiple xml node and print out the values to new file with comma seperated. would like to know how this would be done using either sed/perl or some unix script. an example would be tremendous... sample input file:... (2 Replies)
Discussion started by: davidsouk
2 Replies

10. Shell Programming and Scripting

shell call xalan to transform xml with xslt, but can not find root node

hi experts: i am using xslt to transform xml in shell. But can not find root node of source xml, if i remove the naming space definition in source xml, it works fine. So our the question is how to let xslt know the naming space of srouce xml file? Thanks for your kindly help in... (0 Replies)
Discussion started by: summer_cherry
0 Replies
Login or Register to Ask a Question