如何使用Twig选择singelnode nestet。和我的$ var =? (将来我可以添加数学公式)(使用Shell时:$ xbcom =(xml $ xdxml).selectSingleNode(“city / bases / @ com”)。$ xt)
#!perl
use strict;
use warnings;
use CGI;
use XML::Twig;
my $t = XML::Twig->new(
pretty_print => 'indented',
twig_handlers => {
'city/name' => sub { print $_[1]->text, "\n"; },
'city/population' => sub { print "Pop: ", $_[1]->text, "\n"; },
}
);
$t->parseurl('http://athome.myminicity.com/xml');
<?xml version="1.0" encoding="UTF-8"?><city>
<host>athome.myminicity.com</host>
<name>AtHome</name>
<region code="FR">france</region>
<ranking>2</ranking>
<population>19038934</population>
<incomes>-2147483648</incomes>
<unemployment>0</unemployment>
<transport>100</transport>
<criminality>0</criminality>
<pollution>0</pollution>
<nextnuke>1</nextnuke>
<signatures>0</signatures>
<bases com="381162" env="4118994" ind="6219447" sec="4759785" tra="3807867"/>
</city>
我有工作:
#!perl
use strict;
use warnings;
use CGI;
use XML::Twig;
my $t= XML::Twig->new(pretty_print => 'indented',
twig_handlers => {
'city/name'=>sub{ print " ",$_->text,"-Mmc\n\n"; },
'city/population'=>sub{ print " Population: ",$_->text,"\n"; },
'city/bases'=>sub{ print " Commerce: ",$_->att('com'),"; Environ: ",$_->att('env'),"; Security: ",$_->att('sec'),"; transport: ",$_->att('tra'),"; Industrie: ",$_->att('ind'),"\n\n", },
}
);
## Html future ....
print "Content-type:text/html; charset=utf-8\r\n\r\n";
## Intro
print "\n XML Data Info:\n\r\n";
## Date time stamp
my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime(time);
print " Date:\t$day-".++$month. "-".($yr19+1900)."\n";
print " Time:\t".sprintf("%02d",$hour).":".sprintf("%02d",$min).":".sprintf("%02d",$sec)."\n\n";
## Result (processe)
$t->parseurl('http://athome.myminicity.com/xml');
但我找不到所有@r
答案 0 :(得分:1)
所以,如果您正在寻找:
city/bases/@com
这是一个xpath表达式,它提取属性&#39; com&#39;来自元素city/bases
。
因此,使用XML::Twig
访问此方式的方式是
#!perl
use strict;
use warnings;
use CGI;
use XML::Twig;
my $t = XML::Twig->new(
pretty_print => 'indented',
twig_handlers => {
'/city/name' => sub { print $_->text, "\n"; },
'/city/population' => sub { print "Pop: ", $_->text, "\n"; },
'/city/bases' => sub { print "Bases com: ", $_->att('com'), "\n" },
}
);
$t->parseurl ( 'http://athome.myminicity.com/xml' );
您可能希望稍微扩展sub以测试该属性的存在,但是希望能让您了解它是如何工作的?知道什么可能有用 - XML::Twig
将两组数据解析为它的处理程序:
@_
是$twig
和$element
。
$_
是$element
。
所以你不需要$_[1]
那里。
这将打印匹配属性的每个实例,而不是单个属性,但我确定如果需要,您可以弄清楚如何处理。
但是,您可能根本不想使用twig_handlers
来完成这项工作:
my $city = $t->root;
print "Name: ", $city->field('name'), "\n";
print "Pop:", $city->field('population'), "\n";
print "COM:", $city->first_child('bases')->att('com'), "\n";
或者回答我认为你的问题:
my $xbcom = $t -> root -> first_child('bases') -> att('com');
答案 1 :(得分:1)
我不明白您的问题意味着什么,但看起来这至少会获取您的数据
use strict;
use warnings;
use URI;
use XML::Twig;
my $url = URI->new('http://athome.myminicity.com/xml');
my $twig = XML::Twig->new;
$twig->parseurl($url);
for my $city ( $twig->findnodes('/city') ) {
printf "City: %s\n", $city->field('name');
printf "Population: %s\n", $city->field('population');
my $bases = $city->first_child('bases');
print "Bases:\n";
for my $base ( sort $bases->att_names ) {
printf " %s = %s\n", $base, $bases->att($base);
}
}
City: AtHome
Population: 19039137
Bases:
com = 381162
env = 4119042
ind = 6219519
sec = 4759834
tra = 3807909