简体   繁体   中英

Shell Script to parse JTL/XML file to know if a specific node has a value

I have a JTL file which I need to parse to know if a node named < failure> has true , as its value.

I need to call a script file which should be able to make this decision in a if condition, based on which I need to do other actions. How do I implement the parsing of JTL/XML to know if a failure-true exists or not?

This JTL file has a lot of < failure> nodes in it.

Edit: I may be constructing all this wrong. All am trying is a shell script which will do a specific action if the JTL file it parsed has a node < failure> with value as true .

<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample t="153" lt="152" ts="1434726402307" s="true" lb="xyz" dt="text" by="14"/>
<httpSample t="169" lt="169" ts="1434726402603" s="true" lb="asdasd" dt="text" by="471">
<assertionResult>
<name>Response Assertion</name>
<failure>false</failure>
<error>false</error>
</assertionResult>
</httpSample>
.
.
.

And so it continues

grep for <failure>true</failure> in the .jtl file.

if grep -q <failure>true</failure> file.jtl; then
    echo found
else
    echo not found
fi

If you do not care about which node has the value then this can be accomplished with grep.

if grep -q '<failure>true<\/failure>' jmeter-test.jtl ;then
   echo "FAILURE"
fi

The safe way to do this is with an XML-aware tool:

failure=$(xmlstarlet sel -t -m '//assertionResult/failure' -v . -n <jmeter-test.jtl)
if [[ $failure = true ]]; then
  echo "failed"
else
  echo "success"
fi

Unlike the naive grep approach, this only recognizes failure if it's in the right place -- under an assertionResult and not in a comment, not in text taken from a program's output, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM