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

<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

<name>

tag under

<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

JMSModulename:-UMSJMSSystemResource
Targetserver:-soa_server1,bam_server1

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

<name>

tag under

<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

JMSModulename:-UMSJMSSystemResource
Subdeploymentname:-UMSJMSServer522129776
TargetServer:-UMSJMSServer_auto_1

and one more file for second

<sub-deployemnt>

tag

" UMSJMSServer1709690790_subdeployment.Properties " and inside this file

JMSModulename:-UMSJMSSystemResource
Subdeploymentname:-UMSJMSServer1709690790
TargetServer:-UMSJMSServer_auto_2

Here is the code which i have tried

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,

How about

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.

1 Like

Thanks RudiC .. That worked like a gem...:b:

Hello Rudi,

From the following output

---------- 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

JMSModulename: UMSJMSSystemResource

where we need to have the following

JMSModulename:UMSJMSSystemResource

Thanks

Yes. Remove the "," in the print statements or set OFS="".

1 Like