我正在尝试为Articulate电子教学课程(imsmanifest.xml)解析XML清单文件。
下面提供了XML结构的摘录(我正在尝试深入研究adlcp:masteryscore):
<?xml version="1.0" encoding="UTF-8"?>
<manifest xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" version="1.0" identifier="Electrical_Design_Part_3">
<metadata/>
<organizations default="Electrical_Design_Part_3_ORG">
<organization identifier="Electrical_Design_Part_3_ORG">
<title>Electrical Design - Part 3</title>
<item identifier="Electrical_Design_Part_3_SCO" identifierref="Articulate_Presenter_RES" isvisible="true">
<title>Electrical Design - Part 3</title>
<adlcp:masteryscore>65</adlcp:masteryscore>
</item>
</organization>
</organizations>
<resources/>
</manifest>
我尝试过使用XML :: Simple和XML :: LibXML。我可以使用更简单的XML文件使这些模块正常工作,但不是我实际需要解析的清单文件。
以下代码显示了我尝试使用XML :: LibXML深入查看标题标记:
use XML::LibXML;
$filename = "imsmanifest.xml";
$parser = XML::LibXML->new();
$xmldoc = $parser->parse_file($filename);
for my $sample ($xmldoc->findnodes('/manifest/organizations/organization/item/title')) {
for my $property ($sample->findnodes('./*')) {
print $property->nodeName(), ": ", $property->textContent(), "\n";
}
print "\n";
};
如何处理adlcp中的冒号:masteryscore标签?每当我尝试使用它时,我都会收到错误 - 但也许我做得不对。
有人可以告诉我正确的方法来深入研究adlcp:masteryscore吗?
非常感谢。
答案 0 :(得分:4)
您要求在null命名空间中找到名为manifest
的元素,但是您需要manifest
命名空间中名为http://www.imsproject.org/xsd/imscp_rootv1p1p2
的元素。
修正:
use strict;
use warnings;
use XML::LibXML qw( );
use XML::LibXML::XPathContext qw( );
my $xml_qfn = 'imsmanifest.xml';
my $parser = XML::LibXML->new( no_network => 1 );
my $doc = $parser->parse_file($xml_qfn);
my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs( a => "http://www.adlnet.org/xsd/adlcp_rootv1p2" );
$xpc->registerNs( i => "http://www.imsproject.org/xsd/imscp_rootv1p1p2" );
for my $item ($xpc->findnodes('/i:manifest/i:organizations/i:organization/i:item', $doc)) {
my $title = $xpc->find('i:title/text()', $item);
my $mastery = $xpc->find('a:masteryscore/text()', $item);
print "$title: $mastery\n";
}
注意:在XPath(a
和i
)中使用的实际前缀选择是任意的。您可以选择所需的任何内容,就像编写XML文档一样。
注意:我添加了no_network => 1
以防止每次解析XML文档时libxml都会获取DTD。
答案 1 :(得分:0)
第一步,修复你的例子,使其正确形成xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd" version="1.0" identifier="Electrical_Design_Part_3">
<metadata>
<organizations default="Electrical_Design_Part_3_ORG">
<organization identifier="Electrical_Design_Part_3_ORG">
<title>Electrical Design - Part 3</title>
<item identifier="Electrical_Design_Part_3_SCO" identifierref="Articulate_Presenter_RES" isvisible="true">
<title>Electrical Design - Part 3</title>
<adlcp:masteryscore>65</adlcp:masteryscore>
</item>
</organization>
</organizations>
<resources/>
</metadata>
</manifest>
启动perl调试器
DB<2> use XML::Simple
DB<3> $x=XMLin("example.xml")
DB<4> x $x
0 HASH(0x2733c48)
'identifier' => 'Electrical_Design_Part_3'
'metadata' => HASH(0x2733828)
'organizations' => HASH(0x2733288)
'default' => 'Electrical_Design_Part_3_ORG'
'organization' => HASH(0x272d7e8)
'identifier' => 'Electrical_Design_Part_3_ORG'
'item' => HASH(0x27285f8)
'adlcp:masteryscore' => 65
'identifier' => 'Electrical_Design_Part_3_SCO'
'identifierref' => 'Articulate_Presenter_RES'
'isvisible' => 'true'
'title' => 'Electrical Design - Part 3'
'title' => 'Electrical Design - Part 3'
'resources' => HASH(0x27333d8)
empty hash
'version' => 1.0
'xmlns' => 'http://www.imsproject.org/xsd/imscp_rootv1p1p2'
'xmlns:adlcp' => 'http://www.adlnet.org/xsd/adlcp_rootv1p2'
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
'xsi:schemaLocation' => 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'
DB<6> x keys %$x
0 'xmlns'
1 'xmlns:xsi'
2 'identifier'
3 'version'
4 'metadata'
5 'xsi:schemaLocation'
6 'xmlns:adlcp'
DB<9> x keys %{$x->{metadata}}
0 'resources'
1 'organizations'
DB<10> x keys %{$x->{metadata}{organizations}}
0 'default'
1 'organization'
DB<11> x keys %{$x->{metadata}{organizations}{organizations}
Missing right curly or square bracket at (eval 22)[/usr/share/perl/5.14/perl5db.pl:640] line 4, at end of line
syntax error at (eval 22)[/usr/share/perl/5.14/perl5db.pl:640] line 4, at EOF
DB<12> x keys %{$x->{metadata}{organizations}{organizations}}
empty array
DB<13> x keys %{$x->{metadata}{organizations}{organization}}
0 'identifier'
1 'item'
2 'title'
DB<14> x keys %{$x->{metadata}{organizations}{organization}{item}}
0 'identifier'
1 'identifierref'
2 'isvisible'
3 'title'
4 'adlcp:masteryscore'
DB<19> x $x->{metadata}{organizations}{organization}{item}{'adlcp:masteryscore'}
0 65
DB<20>
所以你要做的就是
use XML::Simple;
$x=XMLIN("example.xml");
print $x->{metadata}{organizations}{organization}{item}{'adlcp:masteryscore'};
希望这有帮助
答案 2 :(得分:0)
xml无效,您需要关闭标签元数据和资源
之后,XML :: Simple将使用此代码
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use XML::Simple qw(:strict);
my $ref = XMLin('test.xml',ForceArray => [], KeyAttr => {});
print STDERR Dumper $ref;