【问题标题】:cannot account for all special characters in XML无法解释 XML 中的所有特殊字符
【发布时间】: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++;
}

有没有更优雅/惯用的方式来转换所有这些神秘人物?

【问题讨论】:

标签: xml perl


【解决方案1】:

这似乎是 URL 百分比编码[1]。

如果您知道哪些字段将对 URL 进行编码,您可以简单地使用某些东西来解码这些“特殊字符”,例如使用 URI::Encode[2] 中的 uri_decode($encoded)。

[1]https://en.m.wikipedia.org/wiki/Percent-encoding

[2]https://metacpan.org/pod/URI::Encode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    相关资源
    最近更新 更多