创建xml文件:使用awk如何创建xml文件 [英] Create xml file : Using awk how to create an xml file

查看:69
本文介绍了创建xml文件:使用awk如何创建xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我简化了我的问题.

我有一个文本文件,其数据如下所示

I have a text file with data as below

package1| class1 | test case1 |  Pass  |  endpoint | ref no  |  
package1| class1 | test case2 |  Pass  |  endpoint | ref no  |  
package2| class2 | test case1 |  Fail  |  endpoint | ref no  | fail reason  
package3| class3 | test case2 |        |           |         |

我想从上面创建一个xml,而如下所示.逻辑,只要field1与上一行的field1不同,就创建一个 节点并测试其下的用例.如果字段4为空,则将测试计为错误.我想在包级别(即 节点)上保留无测试用例,无错误和无故障的计数.并保留测试用例的数量,每个 节点上的错误和失败的数量

I want to create an xml from the above while will be like as below. logic whenever the field1 differs from previous line field1 create a node and tests cases under it. if field 4 is null then the test is counted as error.I want to keep count of no of test cases no of error and no of failure at package level ie node. and also keep count of test cases , no of error and no of failure at each node

<?xml version="1.0" encoding="UTF-8"?>
<testsuites errors="1" failures="1" tests="4">
  <testsuite name="package1" errors="0" failures="0" tests="2">
    <Testcase>
       <class>class1</class>
       <name>Testcase1</name>
       <Result>Pass</Result>
       <FailReason></FailReason>
    </testcase>
    <Testcase>
      <class>class2</class>
      <name>Testcase2</name>
      <Result>Pass</Result>
      <FailReason></FailReason>
      </testcase>
 </TestSuite>

 <testsuite name="package2" errors="0" failures="1" tests="1">
  <Testcase>
    <class>class1</class>
    <name>Testcase1</name>
    <Result>Fail</Result>
    <FailReason>FailReason</FailReason>
  </testcase>
 </TestSuite>

 <testsuite name="package3" errors="1" failures="0" tests="1">
  <Testcase>
    <class>class1</class>
    <name>Testcase1</name>
    <Result>Fail</Result>
    <FailReason>Error</FailReason>
  </testcase>
 </TestSuite>
</Testsuites>

`

我正在使用awk创建尝试了太多代码但没有成功,我不确定应该在其中放置哪些代码,我尝试了将近6个小时来处理它,却无法弄清楚该怎么做并立即干燥.任何帮助表示赞赏.任何脚本解决方案都不错,不仅是awk.

I was using awk to create tried so many code nothing sucessful , i am not sure which code should i put here which i tried almost 6 hours working on it not able to figure out how to do it and dry now. any help is appreciated. any script solution is good not only awk.

推荐答案

请注意,这不是您问题的完整答案.我不愿意一次完整地将整个答案作为一个完整的程序共享:这将导致代码注释太长.

Please note this is not a complete answer to your question. I don't feel comfortable sharing the whole answer as a complete program in one run: it would be way too long code to comment.

请回到我身边,告诉我到目前为止您是否一直在遵循代码,如果出现问题,我将通过编辑答案来添加解释.

Please, come back to me and tell me if you are following the code so far, I will add explanation by editing my answer, if asked.

首先:我先验地删除了原始输入文件中的所有空格.现在看起来像这样:

First of all: I got rid a priori of all the spaces in your original input file. It now looks like this:

package1|class1|test case1|Pass|endpoint|ref no|  
package1|class1|test case2|Pass|endpoint|ref no|  
package2|class2|test case1|Fail|endpoint|ref no|fail reason  
package3|class3|test case2|    |        |      |

然后,我将字段分隔符设置为pipe并继续进行一些处理.正如许多人已经指出的那样,您的输入文件不一致,并且您的xml不兼容.我愿意将所有实际的实施方式留给您(按照SO准则,以便您可以全力以赴)

Then, I set the field separator to pipe and proceed to do some processing. As many have already pointed out, your input file is not consistent, and your xml is not compliant. I am willing to leave to you all the actual implementation (as per SO guidelines, so that you can show your effort)

尝试如下操作:创建一个脚本(您将以 awk -f script.awk textfilewithdata.txt 调用),并使其听起来像:

Try something like this: create a script (that you will call as awk -f script.awk textfilewithdata.txt) and make it sound like:

BEGIN {
    FS = "|"
    numberOfPackages = 0
    fails = "fails"
    testcases = "testcases"
}

{
    allTests++
    if ($1 != currPackage) {
        currPackage = $1
        numberOfPackages++
        if (!(currPackage in packages)) {
            # creates it
            packages[currPackage][testcases] = 0    # stores errors, failures and test
            packages[currPackage][fails] = 0
            }
        packages[currPackage][testcases]++
        if ($4 == "Fail" ) {packages[currPackage][fails]++;allFail++}
    } else {
        packages[currPackage][testcases]++
        if ($4 == "Fail" ) {packages[currPackage][fails]++;allFail++}
    }
}

END {
    print "<?xml version="1.0" encoding="UTF-8"?>"
    print "<testsuites failures=\""allFail"\" tests=\""allTests"\">"
    for (j in packages) {
        print "\t<testsuite name=\""j"\"  failures=\""packages[j][fails]"\" tests=\""packages[j][testcases]"\">"
        for (k in packages[j]) {
            print "\t\t<Testcase>"
            print "\t\t\t<class>"
            print "\t\t<\\Testcase>"
        }
        print "\t<\\testsuite>"
    }
}

再次,请向我提出评论,我将尝试提供有关上述代码的说明.

Again, come back to me with comments, I'll try to provide explanations on the above code.

输出看起来像这样(gawk 4.2,Windows 10)

Output looks like this (gawk 4.2, Windows 10)

<?xml version=1 encoding=-8?>
<testsuites failures="1" tests="4">
        <testsuite name="package1"  failures="0" tests="2">
                <Testcase>
                        <class>
                <\Testcase>
                <Testcase>
                        <class>
                <\Testcase>
        <\testsuite>
        <testsuite name="package2"  failures="1" tests="1">
                <Testcase>
                        <class>
                <\Testcase>
                <Testcase>
                        <class>
                <\Testcase>
        <\testsuite>
        <testsuite name="package3"  failures="0" tests="1">
                <Testcase>
                        <class>
                <\Testcase>
                <Testcase>
                        <class>
                <\Testcase>
        <\testsuite>

这篇关于创建xml文件:使用awk如何创建xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆