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
# 8  
Old 10-11-2017
Hello R.Singh,

Consider the below code
Code:
?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source >
  <name>DataSourceTest</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>DEV_SOAINFRA</value>
      </property>
    </properties>
    <password-encrypted>{AES}xxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>0</initial-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/SOADBDataSource</jndi-name>
  </jdbc-data-source-params>
</jdbc-data-source>

we need the values of the above code into a text file in below format
For Ex:-
Code:
Param1:DataSourceTest
Param2:jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb
Param3:oracle.jdbc.xa.client.OracleXADataSource
Param4:DEV_SOAINFRA

Like wise, we need to map all of the values of XML file into a text file.

Thanks,
Siva

Last edited by Scrutinizer; 10-20-2017 at 03:41 AM.. Reason: Code tags
# 9  
Old 10-11-2017
Hello Siv51427882,

Could you please try following and let me know if this helps you.
Code:
awk -F"[><]" '/<\/jdbc-data-source>/{print "Param1:"param1 RS "Param2:"param2 RS "Param3:"param3 RS "Param4:"param4;param1=param2=param3=param4="";next}  /<jdbc-data-source >/{getline;param1=$3;next} /<url>/{param2=$3;next} /<driver-name>/{param3=$3;next} /<value>/{param4=$3;next}'  Input_file

EDIT: Adding a non-one liner form of solution too now.
Code:
awk -F"[><]" '
/<\/jdbc-data-source>/{
  print "Param1:"param1 RS "Param2:"param2 RS "Param3:"param3 RS "Param4:"param4;
  param1=param2=param3=param4="";
  next
}
/<jdbc-data-source >/{
  getline;
  param1=$3;
  next
}
/<url>/{
  param2=$3;
  next
}
/<driver-name>/{
  param3=$3;
  next
}
/<value>/{
  param4=$3;
  next
}
'   Input_file

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 10-18-2017
Hello R.Singh,

I have executed your code below.

Code:
awk -F"[><]" '
/<\/jdbc-data-source>/{
  print "Param1:"param1 RS "Param2:"param2 RS "Param3:"param3 RS "Param4:"param4;
  param1=param2=param3=param4="";
  next
}
/<jdbc-data-source >/{
  getline;
  param1=$3;
  next
}
/<url>/{
  param2=$3;
  next
}
/<driver-name>/{
  param3=$3;
  next
}
/<value>/{
  param4=$3;
  next
}
'   Input_file


And i got below output.

Code:
Param1:
Param2:jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb
Param3:oracle.jdbc.xa.client.OracleXADataSource
Param4:DEV_SOAINFRA

Param1:- Coming as null.

Here is the input file.

Code:
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="/weblogic/jdbc-data-source" xmlns:sec="/weblogic/security" 
xmlns:wls="/weblogic/security/wls" xmlns:xsi="/XMLSchema-instance" 
xsi:schemaLocation="/weblogic/jdbc-data-source /weblogic/jdbc-data-source/1.2/jdbc-data-source.xsd">

  <name>DataSourceTest</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>DEV_SOAINFRA</value>
      </property>
    </properties>
    <password-encrypted>{AES}xxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>0</initial-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/SOADBDataSource</jndi-name>
  </jdbc-data-source-params>
</jdbc-data-source>


Kindly suggest on this.

Thanks,
Siva

Last edited by Scrutinizer; 10-20-2017 at 03:40 AM..
# 11  
Old 10-18-2017
Quote:
Originally Posted by Siv51427882
.
.
.
Code:
Param1:
Param2:jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb
Param3:oracle.jdbc.xa.client.OracleXADataSource
Param4:DEV_SOAINFRA

Param1:- Coming as null.
.
.
.
Yes, and for a reason: it's because the recent input has a blank line whereas the sample given in post#8 has not.

Please be aware that your specification is far from descriptive or clear (as has been pointed out before). Why don't you tell people that once the <jdbc-data-source tag has been encountered, the <name> tag should go to a line or variable called parameter 1, <url> to parameter 2, etc. ?
And,
Quote:
Originally Posted by Siv51427882
.
.
.
Like wise, we need to map all of the values of XML file into a text file.
.
.
.
doesn't help either. Specify WHAT is taken from where and printed to where.

Last edited by RudiC; 10-18-2017 at 06:11 AM..
# 12  
Old 10-18-2017
Hello Rudi C,

I have a file like below
Code:
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source>
  <name>DataSourceTest</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>DEV_SOAINFRA</value>
      </property>
    </properties>
    <password-encrypted>{AES}xxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>0</initial-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/SOADBDataSource</jndi-name>
  </jdbc-data-source-params>
</jdbc-data-source>


I have executed the below script, which is provided by R.Singh
Code:
awk -F"[><]" '
/<\/jdbc-data-source>/{
  print "Param1:"param1 RS "Param2:"param2 RS "Param3:"param3 RS "Param4:"param4;
  param1=param2=param3=param4="";
  next
}
/<jdbc-data-source >/{
  getline;
  param1=$3;
  next
}
/<url>/{
  param2=$3;
  next
}
/<driver-name>/{
  param3=$3;
  next
}
/<value>/{
  param4=$3;
  next
}
'   Input_file

And the below is the output which i get
Code:
Param1:
Param2:jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb
Param3:oracle.jdbc.xa.client.OracleXADataSource
Param4:DEV_SOAINFRA

At first place, This thread is not allowing me post any weblinks such as xmlns.com,etc
And hence, i have removed the contents from <jdbc-data-source> tag.
Coming to my requirement
Code:
Param1: DataSourceTest
Param2:jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb
Param3:oracle.jdbc.xa.client.OracleXADataSource
Param4:DEV_SOAINFRA
Param5:{AES}xxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
Param6:jdbc/SOADBDataSource

Hope it makes clear now.

Thanks
Siva

Last edited by Scrutinizer; 10-20-2017 at 03:40 AM.. Reason: Added code tags around the Param bits
# 13  
Old 10-18-2017
Sorry, no, it doesn't. Did you read and understand my entire post?
- Which of the samples is the valid one now - the one with the blank line in post#10, or the other ones in #8 and #12? RavinderSingh13's proposal depends on this. And, I'm pretty sure you don't get the result in #12 with the sample input in #12!
- Will "Param1" ALWAYS be "DataSourceTest", "Param2" ALWAYS "jdbc:Oracle:thin:@10.137.181.188:1521/soa12cdb" etc.? If not (which I guess is the case), specify WHAT goes WHERE.

DON'T leave people guessing what your final requirements are!
These 3 Users Gave Thanks to RudiC For This Post:
# 14  
Old 10-20-2017
Hello RudiC,


For Some reasons this thread is NOT allowing me to paste the complete code, since the code contains some web links.

Lemme try from HTML Tag
HTML Code:
<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdbc-data-source/1.2/jdbc-data-source.xsd">
  <name>DataSourceTest</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb</url>
    <driver-name>oracle.jdbc.xa.client.OracleXADataSource</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>DEV_SOAINFRA</value>
      </property>
    </properties>
    <password-encrypted>{AES}xxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=</password-encrypted>
  </jdbc-driver-params>
  <jdbc-connection-pool-params>
    <initial-capacity>0</initial-capacity>
    <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>
  </jdbc-connection-pool-params>
  <jdbc-data-source-params>
    <jndi-name>jdbc/SOADBDataSource</jndi-name>
  </jdbc-data-source-params>
</jdbc-data-source>

we are expecting the following output.
1) Param1 Should be mapped to the value of <name></name>, which is under <jdbc-data-source> tag.

In the above case Param1 should be "DataSourceTest"

2) Param2 should be mapped to the value of <url></url>, which is under<jdbc-data-source-params> tag.

In the above case Param2 should be "jdbc:oracle:thin:@10.137.181.188:1521/soa12cdb"

3) Param3 should be mapped to the value of <driver-name></driver-name>, which is under<jdbc-data-source-params> tag.

In the above case Param3 should be "oracle.jdbc.xa.client.OracleXADataSource"

4) Param4 should be mapped to the value of <value></value>, which is under<jdbc-data-source-params> tag.

In the above case Param4 should be "DEV_SOAINFRA"

5) Param5 should be mapped to the value of <password-encrypted></password-encrypted>, which is under<jdbc-data-source-params> tag.

In the above case Param5 should be "{AES}xxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="

6)Param6 should be mapped to the value of <jndi-name></jndi-name>, which is under<jdbc-data-source-params> tag.

In the above case Param6 should be "jdbc/SOADBDataSource"

Hope it makes clear this time.

Thanks,
Siva

Last edited by Scrutinizer; 10-20-2017 at 03:39 AM.. Reason: Added [noparse] tags to prevent unintentional smileys in the text
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