Extract the specific tags in a XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the specific tags in a XML file
# 29  
Old 10-30-2017
Hello Ravinder Sing,

Please consider below code as input file
Code:
<jdbc-system-resource>
    <name>wlsbjmsrpDataSource</name>
    <target>AdminServer,osb_server1</target>
    <descriptor-file-name>jdbc/wlsbjmsrpDataSource-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>aqjmsuserDataSource</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/aqjmsuserDataSource-1696-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>aqSample</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/aqSample-8170-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>DataSourceTest</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/DataSourceTest-8855-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>OSC_WC_JDBCDataSource</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/OSC_WC_JDBCDataSource-8237-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>coe_31machine</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/coe_31machine-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>SampleDBDataSource</name>
    <target>AdminServer,osb_server1,soa_server1</target>
    <descriptor-file-name>jdbc/SampleDBDataSource-9181-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>HEWR Data Source</name>
    <target>AdminServer</target>
    <descriptor-file-name>jdbc/HEWR_Data_Source-8983-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>94machineDB</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/94machineDB-5930-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>FileAdapterDS</name>
    <target>AdminServer,soa_server1</target>
    <descriptor-file-name>jdbc/FileAdapterDS-7294-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>PolycomTest</name>
    <target>AdminServer,soa_server1</target>
    <descriptor-file-name>jdbc/PolycomTest-0055-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>scottDS</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/scottDS-4414-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>IEXPAPPS</name>
    <target>soa_server1</target>
    <descriptor-file-name>jdbc/IEXPAPPS-0246-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>
  <jdbc-system-resource>
    <name>RoboticsDS</name>
    <target>AdminServer</target>
    <descriptor-file-name>jdbc/RoboticsDS-7006-jdbc.xml</descriptor-file-name>
  </jdbc-system-resource>

Expected output:-
for each <jdbc-system-resource> tag in above code,
1) The script has to create a new file with a name of '<name>.properties'
2) And in that file, the script has to map parameters like below
target servers = the content of <target></target>
descriptor file name = the content of <descriptor-file-name></descriptor-file-name>


Consider the first tag

<jdbc-system-resource>
<name>wlsbjmsrpDataSource</name>
<target>AdminServer,osb_server1</target>
<descriptor-file-name>jdbc/wlsbjmsrpDataSource-jdbc.xml</descriptor-file-name>
</jdbc-system-resource>

For this snippet, the expected output is like below

1) The script has create a new file called wlsbjmsrpDataSource.properties
2) In the wlsbjmsrpDataSource.properties file,
target servers = AdminServer,osb_server1
descriptor file name = jdbc/wlsbjmsrpDataSource-jdbc.xml
3) The script has to repeat the same for each and every <jdbc-system-resource>


Thanks
Siva
# 30  
Old 10-30-2017
Hello Siv51427882,

I would like to request you to please open a NEW Thread for any new kind of questions because it may be confusing for people. Following may help you in same and let me know in case of any queries.
Code:
awk -v s1="target servers = " -v s2="descriptor file name = "  -F"[><]" '
flag && /<\/jdbc-system-resource>/{
  flag="";
  print val1 ORS val2 > file;
  close(file)
}
/<jdbc-system-resource>/{
  flag=1;
  next
}
flag && /<name>/{
  file=$3".properties";
  next
}
flag && /<target>/{
  val1=s1 $3;
  next
}
flag && /<descriptor-file-name>/ && flag{
  val2=s2 $3;
  next
}
'   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 31  
Old 10-30-2017
Sure.
I will create a new thread(s) going forward.
And last script also successfully.
With that being said, this thread can be closed or resolved.

Thanks
Siva
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

How can we extract specific elements from XML?

Hi, I have a requirement to extract specific element value dynamically from XML message. Here is the sample message: <File> <List> <main> <dir>doc/store834/archive</dir> <count>5</count> </main> <main> <dir>doc/store834/extract</dir> <count>6</count> </main> <main> ... (3 Replies)
Discussion started by: renukeswar
3 Replies

3. Shell Programming and Scripting

Parse xml in shell script and extract records with specific condition

Hi I have xml file with multiple records and would like to extract records from xml with specific condition if specific tag is present extract entire row otherwise skip . <logentry revision="21510"> <author>mantest</author> <date>2015-02-27</date> <QC_ID>334566</QC_ID>... (12 Replies)
Discussion started by: madankumar.t@hp
12 Replies

4. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

5. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

6. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

bash extract all occurences delimited from <name> and </name> tags from an xml file

I need to extract all text delimited from <name> and </name> tags from an xml file, but not only first occurence. I need to extract all occurences. I've tried with this command: awk -F"<name>|</name>" 'NF>2{print $2}' but it give only first occurence. How can i modify it? (18 Replies)
Discussion started by: ingalex
18 Replies

9. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

10. Shell Programming and Scripting

extract specific data from xml format file.

Hi, I need to extract the start time value (bold, red font) under the '<LogEvent ID="Timer Start">' tag (black bold) from a file with the following pattern. There are other LogEventIDs listed in the file as well, making it harder for me to extract out the specific start time that I need. . .... (7 Replies)
Discussion started by: 60doses
7 Replies
Login or Register to Ask a Question