【问题标题】:how can I parse a xml file我怎样才能解析一个xml文件
【发布时间】:2017-03-29 05:03:51
【问题描述】:

我正在尝试解析 XML 文件。我从这里下载数据 http://mips.helmholtz-muenchen.de/proj/ppi/

我使用了这段代码,但我得到了错误

use strict;
use warnings;
use XML::Twig;

my $MIPS_file = $ARGV[0];
my $xml = XML::Twig->new();
my $data = $xml->XMLin("$MIPS_file");
my $intList = $data->{'entry'}->{'interactionList'}->{'interaction'};
foreach my $int (@{$intList}) {
  my $experiment_type = $int->{'experimentList'}->{'experimentDescription'}->{'interactionDetection'}->{'names'}->{'shortLabel'};
  my $partList = $int->{'participantList'}->{'proteinParticipant'};
  my ($p1,$p2);
  foreach my $protPart(@{$partList}) {
      if ($protPart->{'proteinInteractor'}->{'organism'}->{'ncbiTaxId'} eq "9606") { # select human proteins
    if (!$p1) {
      $p1 = $protPart->{'proteinInteractor'}->{'xref'}->{'primaryRef'}->{'id'};
    }
    else {
      $p2 = $protPart->{'proteinInteractor'}->{'xref'}->{'primaryRef'}->{'id'};
    }
      }
  }
  print "$p1\$p2\n";
}

我将文件放在桌面(mac)的文件夹中然后我打开终端并调用程序,如perl myfile.pl

这是我得到的错误

在@INC 中找不到 XML/Simple.pm(@INC 包含:/Users/admin/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0/darwin-2level /Users /admin/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0 /Users/admin/perl5/perlbrew/perls/perl-5.16.0/lib/5.16.0/darwin-2level /Users /admin/perl5/perlbrew/perls/perl-5.16.0/lib/5.16.0 .) 在 myfile.pl 第 3 行。 BEGIN 失败——编译在 myfile.pl 第 3 行中止。

安装 twig 后,现在我收到此错误

Use of uninitialized value $MIPS_file in string at myfile.pl line 7.
Can't locate object method "XMLin" via package "XML::Twig" at myfile.pl line 7.

【问题讨论】:

  • 错误是什么?
  • 你必须从 CPAN 安装 XML::Simple
  • 或者更好,不要XML::Simple is discouraged,而是使用XML::TwigXML::LibXML
  • 您使用的是 Windows 还是 Unix?您使用的具体文件是什么? (网址)。
  • cpan install XML::Twig 或者查看包管理器。这可能是相关的:apple.stackexchange.com/questions/75263/…

标签: xml perl


【解决方案1】:

XML::Simple 不是标准 Perl 安装的一部分。如果你想使用它,那么你需要安装它。 This answer 很好地概述了如何做到这一点。

但是,您应该阅读documentation for XML::Simple,它说:

不鼓励在新代码中使用此模块。其他模块也可以提供更直接和一致的接口。特别是,强烈推荐XML::LibXMLXML::Twig 是一个很好的选择。

我强烈建议您放弃对 XML::Simple 的使用,转而使用上述其他模块之一。

更新:您现在已经安装了 XML::Twig,并更新了您的问题以添加您收到的错误消息。

在 myfile.pl 第 7 行的字符串中使用未初始化的值 $MIPS_file。

无法在 myfile.pl 第 7 行通过包“XML::Twig”定位对象方法“XMLin”。

第 7 行似乎是这样的:

my $data = $xml->XMLin("$MIPS_file");

变量$MIPS_file在这一行前面几行被赋予了一个值:

my $MIPS_file = $ARGV[0];

@ARGV 数组是您可以访问传递给程序的任何命令行参数的地方。 $MIPS_file 包含 undef 的事实强烈暗示没有将任何参数传递给您的程序。你需要像这样运行它:

myfile.pl name_of_your_xml_file.xml

第二个错误更有趣。

无法在 myfile.pl 第 7 行通过包“XML::Twig”定位对象方法“XMLin”。

您已从使用 XML::Simple 切换到使用 XML::Twig。但要做到这一点,您只需更改程序中的use 行。您没有更改任何实际代码。 XML::Simple 和 XML::Twig 是完全不同的库。它们根本不以相同的方式工作。 XML::Twig 没有 XMLIn() 方法。您需要阅读 XML::Twig 的文档并更改您的代码以使用该模块提供的各种功能。

【讨论】:

  • 一旦使用了合理的解析器,Stack Overflow 是一个很好的地方,可以回顾一下如何轻松完成它。
  • @Dave Cross 在我安装了 twig 之后,我又遇到了错误,这就是为什么我没有接受你的回答。我在上面修改了我的问题
【解决方案2】:

不知道你下载的是哪个网址,我无法给你一个确切的答案。

然而,一个非常粗略的 XML::Twig 示例 可能 可以满足您的需求:

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;

my $MIPS_file = $ARGV[0];
my $xml = XML::Twig->new();
$xml -> parsefile ( $MIPS_file );

#assuming ncbTaxId is an attribute - I don't know, this is part of the problem with XML::Simple
foreach my $element ( $xml -> get_xpath ( '//proteinInteractor/organism[@ncbiTaxId="9606"]/..' ) ) {
    $element -> print; #debugging;
    #assuming 'id' is an attrbute of 'primaryRef' subelement. 
    print $element -> get_xpath('.//primaryRef',0) -> att('id'); 
}

注意 - 这是基于您的 XML::Simple 代码的猜测,而不是引用源 XML(因为我不知道您使用的是哪个 XML 源)。这是XML::Simple 问题的一部分——它不能完全表示 XML(至少,不是很容易)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    相关资源
    最近更新 更多