Extracting specific fields from an XML file

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extracting specific fields from an XML file
# 1  
Old 03-19-2018
Extracting specific fields from an XML file

Hello All,

I have a requirement to split the input.xml file different files and i have tried using earlier examples(where i have posted in the forum), but still no luck
Here is my input.xml
Code:
<jms-system-resource>
    <name>UMSJMSSystemResource</name>
    <target>soa_server1,bam_server1</target>
    <sub-deployment>
      <name>UMSJMSServer522129776</name>
      <target>UMSJMSServer_auto_1</target>
    </sub-deployment>
    <sub-deployment>
      <name>UMSJMSServer1709690790</name>
      <target>UMSJMSServer_auto_2</target>
    </sub-deployment>
    <descriptor-file-name>jms/UMSJMSSystemResource-jms.xml</descriptor-file-name>
  </jms-system-resource>

For the above file we need to have two different files

Output file 1
**************
File name should be the value of
Code:
<name>

tag under
Code:
<jms-system-resource>

tag.
and it needs to have two parameters namley JMSModulename and Targetserver
For Ex:- for the above input.xml file the expected outputfile1 should be
"UMSJMSSystemResource.Properties" and inside this file
Code:
JMSModulename:-UMSJMSSystemResource
Targetserver:-soa_server1,bam_server1

Output File2:-
****************
File name should be the value of
Code:
<name>

tag under
Code:
<sub-deployment>

tag.
and it needs to have three parameters namely JMSModulename,Subdeployemntname and Targetservers
For Ex:- for the above input.xml file the expected outputfile2 should be

"UMSJMSServer522129776_subdeployment.Properties" and inside this file
Code:
JMSModulename:-UMSJMSSystemResource
Subdeploymentname:-UMSJMSServer522129776
TargetServer:-UMSJMSServer_auto_1

and one more file for second
Code:
<sub-deployemnt>

tag

"UMSJMSServer1709690790_subdeployment.Properties" and inside this file
Code:
JMSModulename:-UMSJMSSystemResource
Subdeploymentname:-UMSJMSServer1709690790
TargetServer:-UMSJMSServer_auto_2

Here is the code which i have tried

Code:
awk -v s1="JMSModuleName = " -v s2="TargetServer = "  -F"[><]" '
flag && /<\/jms-system-resource>/{
  flag="";
  print val1 ORS val2 > file;
  close(file)
}
/<jms-system-resource>/{
  flag=1;
  next
}
flag && /<name>/{
  file=$3".properties";
  val1=s1 $3
  next
}
flag && /<target>/{
  val2=s2 $3;
next
}

'   Test_data.xml

please provide some assistance on this.

Thanks,

Last edited by RudiC; 03-19-2018 at 11:26 AM..
# 2  
Old 03-19-2018
How about
Code:
awk '
/<.?jms-system-resource>/       {JSR = /<j/
                                 next
                                }

/<.?sub-deployment>/            {SDP = /<s/
                                 next
                                }

/<name>/                        {NAM = 1
                                }
/<target>/                      {TGT = 1
                                }

                                {sub (/^ *<[^>]*>/, _)
                                 sub (/<.*$/, _)
                                }

JSR && NAM                      {FN = $0 (SDP?"_subdeployment":"") ".properties"
                                 if (!SDP) MOD = $0
                                 print "JMSModulename:", MOD  >  FN
                                 if (SDP) print "Subdeploymentname:", $0  >  FN
                                 NAM = 0
                                }
JSR && TGT                      {print "TargetServer:", $0  >  FN
                                 TGT = 0
                                }

' file
cf *prop*

---------- UMSJMSServer1709690790_subdeployment.properties: ----------

JMSModulename: UMSJMSSystemResource
Subdeploymentname: UMSJMSServer1709690790
TargetServer: UMSJMSServer_auto_2

---------- UMSJMSServer522129776_subdeployment.properties: ----------

JMSModulename: UMSJMSSystemResource
Subdeploymentname: UMSJMSServer522129776
TargetServer: UMSJMSServer_auto_1

---------- UMSJMSSystemResource.properties: ----------

JMSModulename: UMSJMSSystemResource
TargetServer: soa_server1,bam_server1

cf is my personal cat files scriptlet.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-19-2018
Thanks RudiC .. That worked like a gem...Smilie
# 4  
Old 03-27-2018
Hello Rudi,

From the following output
Code:
---------- UMSJMSServer1709690790_subdeployment.properties: ----------

JMSModulename: UMSJMSSystemResource
Subdeploymentname: UMSJMSServer1709690790
TargetServer: UMSJMSServer_auto_2

---------- UMSJMSServer522129776_subdeployment.properties: ----------

JMSModulename: UMSJMSSystemResource
Subdeploymentname: UMSJMSServer522129776
TargetServer: UMSJMSServer_auto_1

---------- UMSJMSSystemResource.properties: ----------

JMSModulename: UMSJMSSystemResource
TargetServer: soa_server1,bam_server1

Is there a way to remove the space between the field and its values
For ex:-
we are getting the following output
Code:
JMSModulename: UMSJMSSystemResource

where we need to have the following
Code:
JMSModulename:UMSJMSSystemResource

Thanks
# 5  
Old 03-27-2018
Yes. Remove the "," in the print statements or set OFS="".

Last edited by RudiC; 03-28-2018 at 02:57 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in extracting data from XML File

Hi All My input file is an XML and it has some tags and data rows at end. Starting of data rows is <rs:data> and ending of data rows is </rs:data>. Within sample data rows (2 rows) shown below, I want to extract data value after equal to sign (until space or "/" sign). So if XML data... (7 Replies)
Discussion started by: vx04
7 Replies

2. Shell Programming and Scripting

Extracting the tag name from an xml file

Hi, My requirement is something like this, I have a xml file that contains some tags and nested tags, <n:tag_name1> <n:sub_tag1>val1</n:sub_tag1> <n:sub_tag2>val2</n:sub_tag2> </n:tag_name1> <n:tag_name2> <n:sub_tag1>value</n:sub_tag1> ... (6 Replies)
Discussion started by: Little
6 Replies

3. Shell Programming and Scripting

Reading XML file and extracting value

Dear All, I am reading one XML file to extract value from the particular tag:- Sample xml is below:- <KeyValuePairs> <Key>TestString</Key> <Value>Test12_Pollings</Value> </KeyValuePairs> I want to read the value for the KEY tag and there will be multiple key tags :- awk... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Shell Programming and Scripting

Help in extracting fields from a file

I have an input file with contents like: 203969 OrdAcctCycChg USAGE_DAEMON1 203970 OrdAcctCycChg USAGE_DAEMON2 203971 OrdAcctCycChg USAGE_DAEMON3 203972 OrdAcctCycChg USAGE_DAEMON4 I need to extract variables in first column... (51 Replies)
Discussion started by: Rajesh Putnala
51 Replies

5. Shell Programming and Scripting

Need help in extracting data from xml file

Hello, This is my first post in here, so excuse me if I sound too noob here! I need to extract the path "/apps/mp/installedApps/V61/HRO/hrms_01698_A_qa.ear" from the below xml extract. The path will always appear with the key "binariesURL" <deployedObject... (6 Replies)
Discussion started by: abhishek2386
6 Replies

6. UNIX for Dummies Questions & Answers

Extracting data from an xml file

Hello, Please can someone assist. I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> - <PUTTRIGGER xmlns:xsd="http://www.test.org/2001/XMLSchema" xmlns:xsi="http://www.test.org/2001/XMLSchema-instance" APPLICATIONNUMBER="0501160" ACCOUNTNAME="Mrs S Test"... (15 Replies)
Discussion started by: Dolph
15 Replies

7. UNIX for Dummies Questions & Answers

Extracting values from an XML file

Hello People, I have an xml file from which I need to extract the values of the parameters using UNIX shell commands. Ex : Input is like : <Name>Roger</Name> or <Address>MI</Address> I need the output as just : Roger or MI with the tags removed. Please help. (1 Reply)
Discussion started by: sushant172
1 Replies

8. Shell Programming and Scripting

Extracting fields from file

I am need to extract a number of values from a file, put have now clue how to do this. The file looks like this: # My file Dest=87;CompatibleSystemSoftwareVersion=2.5300-; Dest=87;ImageVersion=000061f3;SystemSoftwareVersion=2.5300;CDN=http://my.backup.com/download.txt;CDN_Timeout=30; I... (3 Replies)
Discussion started by: MagicDude4Eva
3 Replies

9. Shell Programming and Scripting

Extracting a part of XML File

Hi Guys, I have a very large XML feed (2.7 MB) which crashes the server at the time of parsing. Now to reduce the load on the server I have a cron job running every 5 min.'s. This job will get the file from the feed host and keep it in the local machine. This does not solve the problem as... (9 Replies)
Discussion started by: shridhard
9 Replies

10. Shell Programming and Scripting

extracting XML file using sed

Hello folks I want to extract data between certain tag in XML file using 'sed' <xml> ......... .......... <one>XXXXXXXXXXXXXXXXXXXX</one> ...... Anyone ?Thank you (7 Replies)
Discussion started by: pujansrt
7 Replies
Login or Register to Ask a Question