How to extract value from XML payload using Shell script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to extract value from XML payload using Shell script
I have runtime XML file which has predefined format and Its being created in Jenkins workspace. I just want to parse the XML payload by using shell script.
The above payload I have to read the status of test-method if it's "FAIL" then I need to get the "testId" value from that particular test method. The above payload I have 3 test methods only two had status Failed I need to fetch both Test id and assign to variable as like below
Expected Output:
Code:
fetchResult = AS1-TC2,AS1-TC3
I just need to fetch the "testId" of failed test-methods and assign it to variable with comma separated using shell script.
I tried the below lines but It doesn't return the entire test-method tags
Code:
failedTC=`grep "test-method.*FAIL" results.xml | sed -e 's/^.*test-instance-name="(.*)-----.*/\1/'
I have runtime XML file which has predefined format and Its being created in Jenkins workspace. I just want to parse the XML payload by using shell script.
The above payload I have to read the status of test-method if it's "FAIL" then I need to get the "testId" value from that particular test method. The above payload I have 3 test methods only two had status Failed I need to fetch both Test id and assign to variable as like below Expected Output:
Code:
fetchResult = AS1-TC2,AS1-TC3
I just need to fetch the "testId" of failed test-methods and assign it to variable with comma separated using shell script. I tried the below lines but It doesn't return the entire test-method tags
Code:
failedTC=`grep "test-method.*FAIL" results.xml | sed -e 's/^.*test-instance-name="(.*)-----.*/\1/'
I want to return entire <test-method> ... </test-method> for status="FAIL"
While you probably *CAN* do this with a shell-script, it's going to take more than a single line. If you **KNOW** that you'll always get a Pass/fail before the ID, you can simply grep for those things, shovel them into variables, and do a basic match, outputting what you want, which is quick and dirty. And prone to failure. There are XML parsing libraries for perl and python that let you do just this, very easily:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $dom = XML::LibXML->load_xml(location => $InputXML);
foreach my $title ($dom->findnodes('//test-results')) {
# This splits apart the tags and values into variables. Do things with them.
$METHOD = $title->findvalue('./test-method');
$ID = $title->findvalue('./param/value');
....
<grep $METHOD for Fail...if so, output $ID>
Above for example only. Totally untested, but will get you started.
Can you please give sample shell script to solve this problem.
No; we are not going to write your scripts for you, but we will be happy to HELP you. You were given suggestions for how to do this in bash, and provided an example of how to use perl to do it. It is now up to **YOU** to do your own work.
Read the "Question Guidelines" link in my posting signature. Post what you wrote/tried on your own, and tell us where you're stuck and we'll be glad to help.
Can you please give sample shell script to solve this problem.
I was about to give you one possible solution but I'm not so sure now...
So I will go like TB0ne, grep is not the good tool here because it's designed to work with lines so mainly with flat files and not with structured data like xml.
TB0ne suggested you to use Perl|Python but you can as well do it with xmllint or xmlstarlet. Considering the fact that xmlstarlet has a flatter learning curve and that your requirement is not too complicated, I would go with xmllint (I've just done your exercise myself so it works).
I was about to give you one possible solution but I'm not so sure now...
So I will go like TB0ne, grep is not the good tool here because it's designed to work with lines so mainly with flat files and not with structured data like xml.
TB0ne suggested you to use Perl|Python but you can as well do it with xmllint or xmlstarlet. Considering the fact that xmlstarlet has a flatter learning curve and that your requirement is not too complicated, I would go with xmllint (I've just done your exercise myself so it works).
Indeed; both are good tools as well. I prefer perl, but that's just me. Aside from what I already gave the OP, it would probably take less than 10 more lines of code to get what they need.
I have runtime XML file which has predefined format and Its being created in Jenkins workspace. I just want to parse the XML payload by using shell script.
If you want to parse XML you should use a more powerful language such as perl or python which have actual modules for parsing XML.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.