Shell脚本用于解析JTL / XML文件以了解特定节点是否具有值

时间:2015-06-26 15:31:00

标签: xml shell xslt xml-parsing jmeter

我有一个JTL文件,我需要解析它以了解名为<的节点失败>其价值为true

我需要调用一个脚本文件,该脚本文件应该能够在if条件下做出此决定,我需要根据该文件执行其他操作。如何实现JTL / XML的解析以了解failure-true是否存在?

这个JTL文件有很多<失败>其中的节点。

编辑:我可能正在构建所有这些错误。我正在尝试的是一个shell脚本,如果它解析的JTL文件有一个节点<失败>值为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>
.
.
.

所以它继续

3 个答案:

答案 0 :(得分:1)

grep表示.jtl文件中的<failure>true</failure>

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

答案 1 :(得分:1)

如果您不关心哪个节点具有该值,那么可以使用grep完成此操作。

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

答案 2 :(得分:1)

安全的方法是使用支持XML的工具:

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

与天真的grep方法不同,这只会识别failure,如果它在正确的位置 - 在assertionResult下而不是在评论中,而不是在从程序输出中获取的文本中等等。