我有一个简单的Perl脚本,可以从服务器解析下载的XML文件。唯一的问题是我所做的数组似乎只存储了第一组标签。
这是我的代码:
sub test_button_func() {
$ua->cookie_jar($cookie_jar);
# Now make your request
my $server_endpoint = "server_url_link";
$server_endpoint =~ s/\%id\%/$user/g;
# set custom HTTP request header fields
my $req = HTTP::Request->new(GET => $server_endpoint);
$req->header('content-type' => 'application/json');
my $parser = shift;
my $resp = $ua->request($req);
if ($resp->is_success) {
# XML response received from Jazz API.
my $message = $resp->decoded_content;
# Parse the XML downloaded here:
# create object
my $xml = new XML::Simple;
# Read XML file
my @data = $xml->XMLin($message);
my $arraySize = scalar (@data);
# Print out the data we need
for (my $num = 0; $num <= ($arraySize - 1); $num++) {
print @data[0]->{'oslc_disc:entry'}[$num]{'oslc_disc:ServiceProvider'}{'dc:title'};
print "\n";
}
}
else {
print "HTTP GET error code: ", $resp->code, "\n";
print "HTTP GET error message: ", $resp->message, "\n";
print $resp->decoded_content;
}
}
我试图解析的XML是:
<oslc_disc:entry>
<oslc_disc:ServiceProvider>
<dc:title>URL MAIN TITLE</dc:title>
<oslc_disc:details rdf:resource="URL LINK"/>
<oslc_disc:services rdf:resource="URL LINK"/>
<jfs_proc:consumerRegistry rdf:resource="URL LINK"/>
</oslc_disc:ServiceProvider>
</oslc_disc:entry>
所以我的观点是我可以解析第一个“oslc_disc”条目。但是XML文件中不止一个。我该如何解析其余部分?
答案 0 :(得分:4)
不要使用XML库来解析RDF。它会让你感到痛苦。
此外,不要将XML :: Simple用于任何事情。
RDF::Trine是Perl中RDF的当前最新技术。 Attean正在编写以替换它,重量更轻,并且应该提供更好的API,但还没有准备好。
use strict;
use warnings;
use RDF::Trine;
# $base is for resolving any relative URLs.
my $base = 'http://example.com/';
# Parse the data into $model
my $parser = RDF::Trine::Parser::RDFXML->new;
my $model = RDF::Trine::Model->new;
$parser->parse_file_into_model($base, \*DATA, $model);
# Some namespaces for querying the data...
my $oslc = RDF::Trine::Namespace->new('http://open-services.net/xmlns/discovery/1.0/');
my $dc = RDF::Trine::Namespace->new('http://purl.org/dc/terms/');
# Cycle through the objects of oscl_disc:entry
for my $provider ( $model->objects(undef, $oslc->entry) ) {
# Get the title. This returns a list of titles, so we call
# it in list context and get just the first result.
my ($title) = $model->objects($provider, $dc->title);
# Print it.
print "GOT: ", $title->literal_value, "\n";
}
__DATA__
<oslc_disc:ServiceProviderCatalog
xmlns:oslc_disc="http://open-services.net/xmlns/discovery/1.0/"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:jfs_proc="https://jazz.net/xmlns/prod/jazz/process/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
rdf:about="https://localhost:9443/ccm/oslc-scm/catalog.xml"
>
<oslc_disc:entry>
<oslc_disc:ServiceProvider>
<dc:title>Foo Bar Baz</dc:title>
<oslc_disc:details rdf:resource="foo"/>
<oslc_disc:services rdf:resource="bar"/>
<jfs_proc:consumerRegistry rdf:resource="baz"/>
</oslc_disc:ServiceProvider>
</oslc_disc:entry>
<oslc_disc:entry>
<oslc_disc:ServiceProvider dc:title="Foo Bar Baz II">
<oslc_disc:details rdf:resource="foo2"/>
<oslc_disc:services rdf:resource="bar2"/>
<jfs_proc:consumerRegistry rdf:resource="baz2"/>
</oslc_disc:ServiceProvider>
</oslc_disc:entry>
</oslc_disc:ServiceProviderCatalog>
我还没有使用过Attean,所以可能会有更优雅的方式来做这件事......
use strict;
use warnings;
use Attean::RDF qw(iri);
# $base is for resolving any relative URLs.
my $base = 'http://example.com/';
# Parse the data into $model
my $store = Attean->get_store('Memory')->new;
my $model = Attean::MutableQuadModel->new( store => $store );
my $data = do { local $/; <DATA> }; # slurp filehandle into string
$model->load_triples(RDFXML => iri($base), $data);
# Some namespaces for querying the data...
my $oslc = 'http://open-services.net/xmlns/discovery/1.0/';
my $dc = 'http://purl.org/dc/terms/';
# Cycle through the objects of oscl_disc:entry
for my $provider ( $model->objects(undef, iri("${oslc}entry"))->elements ) {
# Get the title. This returns a list of titles, so we call
# it in list context and get just the first result.
my ($title) = $model->objects($provider, iri("${dc}title"))->elements;
# Print it.
print "GOT: ", $title->value, "\n";
}
__DATA__
<oslc_disc:ServiceProviderCatalog
xmlns:oslc_disc="http://open-services.net/xmlns/discovery/1.0/"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:jfs_proc="https://jazz.net/xmlns/prod/jazz/process/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
rdf:about="https://localhost:9443/ccm/oslc-scm/catalog.xml"
>
<oslc_disc:entry>
<oslc_disc:ServiceProvider>
<dc:title>Foo Bar Baz</dc:title>
<oslc_disc:details rdf:resource="foo"/>
<oslc_disc:services rdf:resource="bar"/>
<jfs_proc:consumerRegistry rdf:resource="baz"/>
</oslc_disc:ServiceProvider>
</oslc_disc:entry>
<oslc_disc:entry>
<oslc_disc:ServiceProvider dc:title="Foo Bar Baz II">
<oslc_disc:details rdf:resource="foo2"/>
<oslc_disc:services rdf:resource="bar2"/>
<jfs_proc:consumerRegistry rdf:resource="baz2"/>
</oslc_disc:ServiceProvider>
</oslc_disc:entry>
</oslc_disc:ServiceProviderCatalog>