【发布时间】:2020-06-07 13:53:15
【问题描述】:
我正在读取一个 XML 文件以删除节奏盒中的重复歌曲。
但是,有很多奇怪的字符,我在文件名中找不到押韵或理由。他们不只是逃脱。
我在 Perl 中使用 XML::Hash。
例如,我发现
%20 表示单个空格。
%5B 表示[
而%5D 表示]
What characters do I need to escape in XML documents? 和 http://www.escapecodes.info/ 接近我想要的,但没有提供任何有用的信息
我正在阅读https://metacpan.org/pod/XML::Hash,但我没有看到关于这些角色的任何建议。
我可以来回转换每个字符,但必须有一种更优雅、更易读的方法。谷歌搜索没有发现任何东西。
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use diagnostics;
use feature 'say';
use autodie ':all';
use File::Slurp;
use XML::Hash;
use DDP;
use utf8::all;
my $xml_converter = XML::Hash -> new();
my $xml_data = read_file('/home/con/Downloads/rhythmdb.xml');
my $xml_hash = $xml_converter->fromXMLStringtoHash($xml_data);
#p $xml_hash->{rhythmdb}{entry}[0];
my %files_found;
my $new_hash;
my $new_index = 0;
foreach my $song_index (0..scalar @{ $xml_hash->{rhythmdb}{entry} } - 1 ) {
my $file;
if (defined $xml_hash->{rhythmdb}{entry}[$song_index]{location}{text}) {
$file = $xml_hash->{rhythmdb}{entry}[$song_index]{location}{text}
} else {
say 'can\'t get file.';
p $xml_hash->{rhythmdb}{entry}[$song_index];
die
}
$file =~ s/^file:\/\///;
# $file =~ s/%20/ /g;
# $file =~ s/%5B/\[/g;
# $file =~ s/%5D/\]/g;
$new_hash->{rhythmdb}{entry}[$new_index] = $xml_hash->{rhythmdb}{entry}[$song_index];
$new_index++;
}
有没有更优雅/惯用的方式来转换所有这些神秘人物?
【问题讨论】:
-
使用URI::Escape 解码百分比编码字符。