arrays - Cannot access all XML elements PERL -
i have simple perl script can parse downloaded xml file server. problem array have made seems store first set of tags.
here code:
sub test_button_func() { $ua->cookie_jar($cookie_jar); # make request $server_endpoint = "server_url_link"; $server_endpoint =~ s/\%id\%/$user/g; # set custom http request header fields $req = http::request->new(get => $server_endpoint); $req->header('content-type' => 'application/json'); $parser = shift; $resp = $ua->request($req); if ($resp->is_success) { # xml response received jazz api. $message = $resp->decoded_content; # parse xml downloaded here: # create object $xml = new xml::simple; # read xml file @data = $xml->xmlin($message); $arraysize = scalar (@data); # print out data need (my $num = 0; $num <= ($arraysize - 1); $num++) { print @data[0]->{'oslc_disc:entry'}[$num]{'oslc_disc:serviceprovider'}{'dc:title'}; print "\n"; } } else { print "http error code: ", $resp->code, "\n"; print "http error message: ", $resp->message, "\n"; print $resp->decoded_content; } }
the xml try parse this:
<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>
so point can parse first "oslc_disc" entry. there more 1 of in xml file. how parse rest?
do not use xml library parse rdf. cause world of pain.
also, don't use xml::simple anything, ever.
rdf::trine current state of art rdf in perl. attean being written replace it, lighter weight, , should offer nicer api, not ready yet.
example using rdf::trine
use strict; use warnings; use rdf::trine; # $base resolving relative urls. $base = 'http://example.com/'; # parse data $model $parser = rdf::trine::parser::rdfxml->new; $model = rdf::trine::model->new; $parser->parse_file_into_model($base, \*data, $model); # namespaces querying data... $oslc = rdf::trine::namespace->new('http://open-services.net/xmlns/discovery/1.0/'); $dc = rdf::trine::namespace->new('http://purl.org/dc/terms/'); # cycle through objects of oscl_disc:entry $provider ( $model->objects(undef, $oslc->entry) ) { # title. returns list of titles, call # in list context , first result. ($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>
example using attean
i've not used attean yet, there may more elegant ways of doing this...
use strict; use warnings; use attean::rdf qw(iri); # $base resolving relative urls. $base = 'http://example.com/'; # parse data $model $store = attean->get_store('memory')->new; $model = attean::mutablequadmodel->new( store => $store ); $data = { local $/; <data> }; # slurp filehandle string $model->load_triples(rdfxml => iri($base), $data); # namespaces querying data... $oslc = 'http://open-services.net/xmlns/discovery/1.0/'; $dc = 'http://purl.org/dc/terms/'; # cycle through objects of oscl_disc:entry $provider ( $model->objects(undef, iri("${oslc}entry"))->elements ) { # title. returns list of titles, call # in list context , first result. ($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>
Comments
Post a Comment