XML Parsing having optional tags into flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML Parsing having optional tags into flat file
# 1  
Old 08-08-2013
XML Parsing having optional tags into flat file

In xml file i have following data where some tags like<ChrgBr> may not be present in every next file.
So i want these values to be stored in some variable like var1="405360,00" , var2="DEBT" and so on
,but if <ChrgBr> tag has no value or is absent var2 should have space like var2=" " so
that i can put the if condition to print only present varable values in the flat file

SAMPLE DATA :1
Code:
        <IntrBkSttlmAmt Ccy="USD">405360,00</IntrBkSttlmAmt>
                <ChrgBr>DEBT</ChrgBr>
                <ChrgsInf>
                        <Amt Ccy=>00.00</Amt>
                        <Agt>
                                <FinInstnId>
                                        <ClrSysMmbId>
                                                <MmbId>BRANCHNO1</MmbId>
                                        </ClrSysMmbId>
                                </FinInstnId>
                        </Agt>
                </ChrgsInf>

SAMPLE DATA :2
Code:
        <IntrBkSttlmAmt Ccy="USD">405360,00</IntrBkSttlmAmt>
                <ChrgsInf>
                        <Amt Ccy=>00.00</Amt>
                        <Agt>
                                <FinInstnId>
                                        <ClrSysMmbId>
                                                <MmbId>BRANCHNO1</MmbId>
                                        </ClrSysMmbId>
                                </FinInstnId>
                        </Agt>
                </ChrgsInf>

Code:
OUTPUT SAMPLE DATA :1 == var1="405360,00"
             var2="DEBT"
             var3="00.00"
             var4="BRANCHNO1"

OUTPUT SAMPLE DATA :2 == var1="405360,00"
             var2=" "
             var3="00.00"
             var4="BRANCHNO1"


Last edited by Scott; 08-08-2013 at 09:08 AM.. Reason: Please use code tags
# 2  
Old 08-08-2013
You could use an awk program something like below, write the output to a file and source the output file:
Code:
awk -F'[<>]' '
        /<IntrBkSttlmAmt/ {
                v1 = $3
        }
        /<ChrgBr/ {
                v2 = $3
        }
        /<Amt/ {
                v3 = $3
        }
        /<MmbId/ {
                v4 = $3
        }
        END {
                print "var1=\""v1"\""
                print "var2=\""v2"\""
                print "var3=\""v3"\""
                print "var4=\""v4"\""
        }
' xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[ask]xml to flat file

dear all, i need your advice, i have xml file like this input.xml <?xml version="1.0" encoding="UTF-8"?> <session xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <capture> <atribut name="tmp_Filename" value="INTest.rbs"/> <atribut name="size_Filename" value="INTest.rbs"/>... (2 Replies)
Discussion started by: zvtral
2 Replies

2. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

3. Shell Programming and Scripting

Help with converting XML to Flat file

Hi Friends, I want to convert a XML file to flat file. Sample I/p: <?xml version='1.0' encoding='UTF-8' ?> <DataFile xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' contactCount='4999' date='2012-04-14' time='22:00:14' xsi:noNamespaceSchemaLocation='gen .xsd'> <Contact... (3 Replies)
Discussion started by: karumudi7
3 Replies

4. Shell Programming and Scripting

Reading XML data in a FLAT FILE

I have a requirement to read the xml file and split the files into two diffrent files in Unix shell script. Could anyone please help me out with this requirement. Sample file --------------- 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Information... (3 Replies)
Discussion started by: kmanivan82
3 Replies

5. Shell Programming and Scripting

To read a flat file containing XML data

I have a file something like this:aaaa.xml content of the file is 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <storeInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s> <BRANCH_NO>3061</BRANCH_NO> <BRANCH_NAME>GREEN EXPRESS</BRANCH_NAME> ... (4 Replies)
Discussion started by: kmanivan82
4 Replies

6. Shell Programming and Scripting

Converting a flat file in XML

Hello Friends, I am new to UNIX shell scripting. Using bash....Could you please help me in converting a flat file into an XML style output file. Flat file: (Input File entries looks like this) John Miller: 617-569-7996:15 Bunting lane, staten Island, NY: 10/21/79: 60600 The... (4 Replies)
Discussion started by: humkhn
4 Replies

7. Shell Programming and Scripting

Continuous Flat file parsing

Hi all, I'm looking for some tips on an ideal method of parsing a huge fixed length flat file (~500gb) into a delimited text file. We have to do this because our data warehouse platform only accepts delimited file loads. In the past, we've done this with SAS (only on smaller ~40GB files) by... (1 Reply)
Discussion started by: cg2
1 Replies

8. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

9. Shell Programming and Scripting

XML to flat file

Hi all, can u please help me in converting any given XML file to flat file. thanks in advance. -bali (2 Replies)
Discussion started by: balireddy_77
2 Replies

10. UNIX for Advanced & Expert Users

XML to flat file in Unix

Hello, How can I take a file in XML format and convert it to a comma separated format? Is there any scripts or programs that can do this for Unix? I tried surfing the net for such an application, but everything seems to be for Windows OS. Any help or suggestions are greatly appreciated. ... (2 Replies)
Discussion started by: oscarr
2 Replies
Login or Register to Ask a Question