将两个标签的属性打印在一起

时间:2015-09-22 00:32:23

标签: perl

我正在使用XML:twig使用Perl从XML文件中提取一些属性;

这是我的代码

import java.io.PrintWriter;
import java.util.Scanner;
import java.io.*;

class OneStandard {
    public static void main(String[] args) throws IOException {

        Scanner input1 = new Scanner(new File("ClaimProportion.txt"));//reads in claim dataset txt file

        Scanner input2 = new Scanner(new File("ClaimProportion.txt"));

        Scanner input3 = new Scanner(new File("ClaimProportion.txt"));


        //this while loop counts the number of lines in the file
        while (input1.hasNextLine()) {
            NumClaim++;
            input1.nextLine();
        }
            System.out.println("There are "+NumClaim+" different claim sizes in this dataset.");
            int[] ClaimSize = new int[NumClaim];

            System.out.println("      ");
            System.out.println("The different Claim sizes are:");

            //This for loop put the first column into an array
        for (int i=0; i<NumClaim;i++){
            ClaimSize[i] = input2.nextInt();
            System.out.println(ClaimSize[i]);
            input2.nextLine();
        }


        double[] ProportionSize = new double[NumClaim];
        //this for loop is trying to put the second column into an array
        for(int j=0; j<NumClaim; j++){
            input3.skip("20");
            ProportionSize[j] = input3.nextDouble();
            System.out.println(ProportionSize[j]);
            input3.nextLine();
        }

    }
}

这打印出以下结果:

use XML::Twig;
my $file = $ARGV[0];
$file =~ /(.+)\.xml/;
my $outfile = $1 . ".snp" ;
open my $out, '>', $outfile or die "Could not open file '$outfile' $!";
my $twig = XML::Twig->new(
    twig_handlers => {
        'Rs/MergeHistory' => \&MergeHistory,
    }
);

$twig -> parsefile( "$file");

sub MergeHistory {
    my ($twig, $elt) = @_;
    print $out "\t";
    print $out "rs";
    print $out $elt->att('rsId'), ",";
    print $out "b";
    print $out $elt->att('buildId'), ",";
}

我想要的是将每个MergeHistory rsId和buildId打印在一起,如下所示:

rs56546490,b130,    rs386588736,b142
rs56546490,b130,    rs386588736,b142

以下是XML文件的一部分,其中包含两个MergeHistory标记:

rs56546490,rs386588736,   b130,b142
rs56546490,rs386588736,   b130,b142

1 个答案:

答案 0 :(得分:-1)

twig_handlers适用于预处理XML,尤其适用于丢弃它。

它可能不是你想要的东西 - 它看起来像你要做的是:

  • 提取每个&#39; MergeHistory&#39;来自每个&#39; Rs&#39;元件。
  • 打印重新格式化的内容。

所以考虑到这一点 - 我认为你可能想要的是findnodeschildren

my $twig = XML::Twig->parsefile( $file );

foreach my $rs ( $twig->findnodes('//Rs') ) {
    print join( ",",
        map { "rs" . $_->att('rsId') } $rs->children('MergeHistory') ),
        "\t";
    print join( ",",
        map { "b" . $_->att('buildId') } $rs->children('MergeHistory') ),
        "\n";
}

根据您的样本,打印出来:

rs56546490,rs386588736  b130,b142

哪个看起来大概是你想要的?

  • 我们使用findnodes来迭代Rs个元素。
  • 在每个内容中,我们使用children来获取MergeHistory元素。
  • map提取属性并连接前面的brs字符串。
  • join以逗号分隔合并。

(如果你愿意,你仍然可以使用twig_handlers进行上述操作,通过触发&#34; Rs&#34;处理器)