Perl 中的 XML::智能解析器

发布于 2024-08-25 08:11:51 字数 1757 浏览 2 评论 0 原文

我正在继续使用过时的 生物信息学书籍,我正在尝试使用XML::智能模块。

我怀疑该模块的方法在 6 年的时间里已经发生了变化,而且我对 Perl 没有经验,无法从 cpan 源。注释掉的代码证明了 ncbi.gov 查询功能,我在使用“新”方法时遇到了麻烦 - 它没有解析 XML。我做错了什么?谢谢!

更新 具体来说,我在解析和显示 Id 数组时遇到了麻烦: my @Id = $results->{eSearchResult}{IdList}{Id}{'@'}; 我在 OSX 终端上运行此脚本,但运行此脚本时没有看到任何 Id。我看到了正确的伯爵。谢谢!

#!/usr/local/bin/perl
# use lib "/Users/fogonthedowns/myperllib";
# use LWP::Simple;
use XML::Smart;
use strict;

#Set base URL for all eutils
my $utils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
my $db = "Pubmed";
my $query ="Cancer+Prostate";
my $retmax = 10;
my $esearch = "$utils/esearch.fcgi?" . 
              "db=$db&retmax=$retmax&term=";

# my $esearch_result = get($esearch.$query);
# print "ESEARCH RESULT: $esearch_result\n";
# print "Using Query: \n$esearch$query\n";
# print "hello world\n";

my $results = XML::Smart->new($esearch.$query,"XML::Parser");
my $count = $results->{eSearchResult}{Count};
my @Id = $results->{eSearchResult}{IdList}{Id}{'@'};
my $all_Id = join("\n", @Id);

print "Count = $count\n";
print "$all_Id\n";

I'm continuing to work out of an outdated bioinformatics book and I'm attempting to use the XML::Smart Module.

I suspect the module's methods have changed over the course of 6 years and I'm inexperienced with perl to troubleshoot from cpan source. The commented out code proves the ncbi.gov query functions, I'm having trouble with the 'new' method - it's not parsing the XML. What am I doing wrong? Thanks!

Update Specifically I'm running into trouble with parsing and displaying the Id array: my @Id = $results->{eSearchResult}{IdList}{Id}{'@'}; I'm running this on OSX terminal and I don't see any Ids when I run this script. I am seeing the proper Count. Thanks!

#!/usr/local/bin/perl
# use lib "/Users/fogonthedowns/myperllib";
# use LWP::Simple;
use XML::Smart;
use strict;

#Set base URL for all eutils
my $utils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
my $db = "Pubmed";
my $query ="Cancer+Prostate";
my $retmax = 10;
my $esearch = "$utils/esearch.fcgi?" . 
              "db=$db&retmax=$retmax&term=";

# my $esearch_result = get($esearch.$query);
# print "ESEARCH RESULT: $esearch_result\n";
# print "Using Query: \n$esearch$query\n";
# print "hello world\n";

my $results = XML::Smart->new($esearch.$query,"XML::Parser");
my $count = $results->{eSearchResult}{Count};
my @Id = $results->{eSearchResult}{IdList}{Id}{'@'};
my $all_Id = join("\n", @Id);

print "Count = $count\n";
print "$all_Id\n";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2024-09-01 08:11:51

你做错的第一件事是注释掉use strict,第二件事是使用-w而不是use warnings

当严格打开时,perl 将报告:

在 tmp:test.pl 第 19 行使用“strict subs”时不允许使用裸字“XML::Parser”。

这让我们可以跟踪问题发生的位置。

文档中的示例表示第二个参数(要使用的解析器)应该被引用了,而你还没有引用它。

所以我们改为:

my $results = XML::Smart->new($esearch.$query,"XML::Parser");

……然后它就运行了。

(顺便说一下,该语言被称为“Perl”,而不是“perl”或“PERL”)

The first thing you have done wrong is to comment out use strict, the second is to use -w instead of use warnings.

With strict turned on, perl will report:

Bareword "XML::Parser" not allowed while "strict subs" in use at tmp:test.pl line 19.

This lets us trace where the problem is occurring.

The examples in the documentation say that the second argument (the parser to use) should be quoted, and you haven't quoted it.

So we change to:

my $results = XML::Smart->new($esearch.$query,"XML::Parser");

… and it runs.

(Incidentally, the language is called "Perl", not "perl" or "PERL")

她比我温柔 2024-09-01 08:11:51

my @Id = $results->{eSearchResult}{IdList}{Id}{'@'};

更改为:

my @Id = $results->{eSearchResult} {IdList}{Id}('@');

change:

my @Id = $results->{eSearchResult}{IdList}{Id}{'@'};

to:

my @Id = $results->{eSearchResult}{IdList}{Id}('@');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文