Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在perl中解析xml网页_Xml_Perl_Xml Parsing - Fatal编程技术网

如何在perl中解析xml网页

如何在perl中解析xml网页,xml,perl,xml-parsing,Xml,Perl,Xml Parsing,您好目前我能够解析xml文件,如果它是保存在我的文件夹从网页 use strict; use warnings; use Data::Dumper; use XML::Simple; my $parser = new XML::Simple; my $data = $parser->XMLin("config.xml"); print Dumper($data); 但是如果我试图从网站上解析它,它就不起作用了 use strict; use warnings; use Data::Du

您好目前我能够解析xml文件,如果它是保存在我的文件夹从网页

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;

my $parser = new XML::Simple;
my $data = $parser->XMLin("config.xml");
print Dumper($data);
但是如果我试图从网站上解析它,它就不起作用了

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;

my $parser = new XML::Simple;
my $data = $parser->XMLin("http://website/computers/computers_main/config.xml");
print Dumper($data);
它给我以下错误“文件不存在:在test.pl第12行”


如何解析网页中的多个xml文件?我必须从网站上获取多个xml并对其进行解析。有人能帮我一下吗?

请阅读相关文档。请注意,
XMLin
方法可以接受文件句柄、字符串,甚至是
IO::handle
对象。它不能接受的是通过HTTP的URL

使用Perl模块获取所需的XML文件,并将其传递到
XMLin


您必须通过使用下载并安装
LWP::Simple
,就像您以前对
XML::Simple

超级编辑所做的那样:此方法需要WWW::Mechanize,但它允许您登录到您的网站,然后获取XML页面。您必须更改评论中的一些内容。希望这有帮助

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use WWW::Mechanize;

# Create a new instance of Mechanize
$bot = WWW::Mechanize->new();
# Create a cookie jar for the login credentials
$bot->cookie_jar(
        HTTP::Cookies->new(
            file           => "cookies.txt",
            autosave       => 1,
            ignore_discard => 1,
    )
);
# Connect to the login page
$response = $bot->get( 'http://www.thePageYouLoginTo.com' );
# Get the login form
$bot->form_number(1);
# Enter the login credentials.
# You're going to have to change the login and 
# pass(on the left) to match with the name of the form you're logging
# into(Found in the source of the website). Then you can put your 
# respective credentials on the right.
$bot->field( login => 'thisIsWhereYourLoginInfoGoes' );
$bot->field( pass => 'thisIsWhereYourPasswordInfoGoes' );
$response =$bot->click();
# Get the xml page
$response = $bot->get( 'http://website/computers/computers_main/config.xml' );
my $content = $response->decoded_content();
my $parser = new XML::Simple;
my $data = $parser->XMLin($content);
print Dumper($data);
试试看。使用LWP::简单,如上所述。它只是连接到页面并获取该页面的内容(xml文件)并通过XMLin运行。 编辑:在get$url行添加了简单的错误检查。 Edit2:将代码保留在此处,因为如果不需要登录,它应该可以工作

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use LWP::Simple;

my $parser = new XML::Simple;

my $url = 'http://website/computers/computers_main/config.xml';
my $content = get $url or die "Unable to get $url\n";
my $data = $parser->XMLin($content);

print Dumper($data);

如果您没有任何特定的理由坚持使用XML::Simple,那么可以使用其他解析器,如XML::Twig、XML::LibXML,它提供了一个内置功能来解析web上可用的XML

下面是使用XML::Twig实现相同功能的简单代码

use strict;
use warnings;
use XML::Twig;
use LWP::Simple;

my $url = 'http://website/computers/computers_main/config.xml';
my $twig= XML::Twig->new();
$twig->parse( LWP::Simple::get( $url ));

如前所述,XML::Simple没有这种内置功能。

问题?这不是问题。XML::Simple也不是。嘿,谢谢你的回复。我试着如上所述,但由于某种原因,我得到错误的说“无法获得网址”。你知道它会出什么问题吗。我已经正确安装了这两个模块。我想这可能是一个错误的URL,只是因为我使用的URL格式与上述相同,它似乎是为我工作。您尝试过其他url吗?您可以通过谷歌“filetype:xmlsomequery”获取一些测试xml文件。只需抓取他们的URL并将其放在上面的脚本中,这样我们就可以查看是您的URL还是脚本。您是正确的。这是身份验证问题。我必须手动登录才能访问该url。有没有办法在perl中通过身份验证并解析所需的url?只是添加了一个新的代码块,您可以进行测试。您应该能够登录,然后获取xml文件。我不确定当我编辑我的答案时StackOverflow是否会通知你,这也是我在这里发表评论的原因(这里是新人)。嘿,谢谢你的帮助。我会试试看。希望它能起作用。嘿,谢谢你的回复,但使用XML::Twig时,我会收到一个错误:“在/ur/lib/perl5/site\u perl/5/10/i686 cygwin/XML/Parser.pm第197行test.pl第16行的第1行第0列,byte-1处找不到元素”知道会出什么问题吗?