module LibXML::XML::Encoding

The encoding class exposes the encodings that libxml supports via constants.

LibXML converts all data sources to UTF8 internally before processing them. By default, LibXML determines a data source’s encoding using the algorithm described on its website.

However, you may override a data source’s encoding by using the encoding constants defined in this module.

Example 1:

io = File.open('some_file', 'rb')
parser = XML::Parser.io(io, :encoding => XML::Encoding::ISO_8859_1)
doc = parser.parse

Example 2:

parser = XML::HTMLParser.file("some_file", :encoding => XML::Encoding::ISO_8859_1)
doc = parser.parse

Example 3:

document = XML::Document.new
document.encoding = XML::Encoding::ISO_8859_1
doc << XML::Node.new

Constants

ASCII

pure ASCII.

EBCDIC

EBCDIC uh!

ERROR

No char encoding detected.

EUC_JP

EUC-JP.

ISO_2022_JP

ISO-2022-JP.

ISO_8859_1

ISO-8859-1 ISO Latin 1.

ISO_8859_2

ISO-8859-2 ISO Latin 2.

ISO_8859_3

ISO-8859-3.

ISO_8859_4

ISO-8859-4.

ISO_8859_5

ISO-8859-5.

ISO_8859_6

ISO-8859-6.

ISO_8859_7

ISO-8859-7.

ISO_8859_8

ISO-8859-8.

ISO_8859_9

ISO-8859-9.

NONE

No char encoding detected.

SHIFT_JIS

Shift_JIS.

UCS_2

UCS-2.

UCS_4BE

UCS-4 big endian.

UCS_4LE

UCS-4 little endian.

UCS_4_2143

UCS-4 unusual ordering.

UCS_4_3412

UCS-4 unusual ordering.

UTF_16BE

UTF-16 big endian.

UTF_16LE

UTF-16 little endian.

UTF_8

UTF-8

Public Class Methods

from_s("UTF_8") → XML::Encoding::UTF_8 click to toggle source

Converts an encoding string to an encoding constant defined on the XML::Encoding class.

static VALUE rxml_encoding_from_s(VALUE klass, VALUE encoding)
{
  xmlCharEncoding xencoding;

  if (encoding == Qnil)
    return Qnil;

  xencoding = xmlParseCharEncoding(StringValuePtr(encoding));
  return INT2NUM(xencoding);
}
encoding_to_rb_encoding(Input::ENCODING) → Encoding click to toggle source

Converts an encoding constant defined on the XML::Encoding class to a Ruby encoding object (available on Ruby 1.9.* and higher).

VALUE rxml_encoding_to_rb_encoding(VALUE klass, VALUE encoding)
{
  xmlCharEncoding xmlEncoding = (xmlCharEncoding)NUM2INT(encoding);
  rb_encoding* rbencoding = rxml_xml_encoding_to_rb_encoding(klass, xmlEncoding);
  return rb_enc_from_encoding(rbencoding);
}
to_s(XML::Encoding::UTF_8) → "UTF-8" click to toggle source

Converts an encoding constant defined on the XML::Encoding class to its text representation.

static VALUE rxml_encoding_to_s(VALUE klass, VALUE encoding)
{
  const xmlChar* xencoding = (const xmlChar*)xmlGetCharEncodingName(NUM2INT(encoding));

  if (!xencoding)
    return Qnil;
  else
    return rxml_new_cstr(xencoding, xencoding);
}