English 中文(简体)
libxml将cent性转化成斜体X越野。 Json不高兴
原标题:libxml converts accented characters into backslash x escapes. Json is not happy

我有以下特性:一毫升一米的读物。 如果我打印出读物,则通常以重心印制。

reader = XML::Reader.new(File.open("somefile.xml", "r"))
reader.read
reader.read
...
p reader.node

=> ... Full_Name="Univisión Network - East Feed" ...

但是,如果我这样做,那就逃了。

p reader.node["Full_Name"]
=> "UnivisixC3xB3n Network - East Feed"

当我试图将这一价值转换为json laater时,我有以下错误。

Encoding::UndefinedConversionError: "xC3" from ASCII-8BIT to UTF-8

本文件的xml行文如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

我没有控制Xml文件本身。 我怎么能将这种单条编码特性重新归为 j子,或改成一种格式 j子?

www.un.org/Depts/DGACM/index_spanish.htm 顺便提一下,这就是它如何看待实际的《禁止杀伤人员地雷公约》文件。

Full_Name="Univisi&#243;n Network - East Feed" 
最佳回答

EDIT
so i ve been trying figuring this out for quite some time now. funny thing: your code works without error in ruby 1.8 (at least here). so i think the error has to do with ruby 1.9 s new encoding handling. somehow it cannot figure out that the parsed and read XML is in (libxml s internal) utf-8 format (the document encoding doesn t matter here: in 1.8 it works with both iso-8859-1 and utf-8, even with the wrong xml encoding declaration). instead, it treats it as ASCII-8BIT, or BINARY. in other words, it doesn t know the encoding. which is why to_json fails trying to convert it to utf-8.

解决该问题最容易的方法可能是将1.8瓦.降为1.8瓦.。

alternatively, your approach of force_encoding( UTF-8 ) seems to be reasonable.
EDIT END

您可以尝试将适当的编码传递给读者:

reader = XML::Reader.new(File.open("somefile.xml", "r"), 
  XML::Encoding::ISO_8859_1)
问题回答

但是,如果这样做的话,就逃了。

情况并非如此。 你所看到的是UTF-8的产出,它被解释为一种 by。

问题是,你的XML文件says,其编号为ISO-8859-1,而实际上是UTF-8。 找出问题,应该发挥作用。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签