如何在sax解析器中跳过字符perl

问题描述:

我在读取perl中的特殊字符时遇到了问题。我有以下XML文件并使用荫SAX解析器其循环各酒店和取值但是当它读酒店介绍它,我们跳过文本,因为我们在1000m�如何在sax解析器中跳过字符perl

<?xml version="1.0" encoding="UTF-8"?> 
<XMLResponse> 
    <ResponseType>HotelListResponse</ResponseType> 
    <RequestInfo> 
     <AffiliateCode>NI9373</AffiliateCode> 
     <AffRequestId>2</AffRequestId> 
     <AffRequestTime>2015-10-29T15:52:05</AffRequestTime> 
    </RequestInfo> 
    <TotalNumber>264234</TotalNumber> 
    <Hotels> 
     <Hotel> 
      <HotelCode>AD0BFU</HotelCode> 
      <OldHotelId>0</OldHotelId> 
      <HotelLocation/> 
      <HotelInfo>Renovated in 2001, Hotel Bringue features a 1000 m� garden and comprises 5 floors with 105 double rooms, 2 suites and 7 single rooms. Hotel Bringue is situated in the picturesque village El Serrat, boasting the most amazing mountain views in the region and just a short drive to the main ski resort of Vallnord.After an exhausting day, you can go for a relaxing swim in the pool, re-energise your body in the jacuzzi or pamper yourself in the sauna. The rooms are beautifully appointed and come with an array of modern amenities for a pleasant stay.</HotelInfo> 
      <HotelTheme>Ski Hotels</HotelTheme> 
     </Hotel> 
    </Hotels> 
</XMLResponse> 

有一个特殊的租船我如何跳过sax解析器中的字符。

如果你想修复这个文件,我不知道为什么这里甚至需要XML解析器。

perl -i~ -pe's/\xC3\xAF\xC2\xBF\xC2\xBD//g' file.xml 

你会如何定义“特殊字符”?一个定义可能是:非ASCII字符。 ASCII字符的范围是0x00 - 0x7f(尽管不是全部在XML中都是有效的)。所以,你可以放弃每一个字符,在这个范围内的东西,如:

$data =~ s/[^\x00-\x7f]//g; 

但是,这是可能会丢掉很多非常好的数据。所有重音字符将被丢弃(例如:“Zürich”中的“ü” - 离开“Zrich”)。货币符号像€,£或¥(甚至¢)将会丢失。您还会失去其他无害的字符,如 - , - ,“,”或•不可见的字符,如非破坏空格。

所以问题是你为什么要放弃这些字符?他们在什么时候成为问题?我注意到你已经标记了'mysql'这个问题 - 当你试图在数据库中插入数据时你会遇到问题吗?你是否正确地声明了数据库的编码?您是否在数据库连接上启用了mysql_enable_utf8?也许你可以在eval块中做你的插入,并且如果插入失败,只应用上面的正则表达式。

另一种选择可能是通过Encoding::FixLatin传递数据。这应该使字符串安全地插入到UTF-8数据库中,即使结果字符不是最初的目的。

顺便说,我认为在上述具体实例中,数据最初表示:

Hotel Bringue features a 1000 m² garden 

SUPERSCRIPT TWO字符是Unicode U + 00B2和UTF-8,将被编码为两个字节:C2 B2。某个进程可能已经读取了这些字节,但将它们解码为Latin-1而不是UTF-8,并且每个字节都变成了一个字符。当数据出现错误的编码声明或人们无法理解如何使用Unicode字符时,这种双重编码可能会反复发生 - 导致一个字符变成垃圾的许多字符。