123456789_123456789_123456789_123456789_123456789_

Module: LibXML::XML::Encoding

Relationships & Source Files
Defined in: ext/libxml/ruby_xml_encoding.c

Overview

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

Constant Summary

Class Method Summary

Class Method Details

.from_s("UTF_8") ⇒ Encoding (mod_func)

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

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_encoding.c', line 49

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

  if (encoding == Qnil)
    return Qnil;

  xencoding = xmlParseCharEncoding(StringValuePtr(encoding));
  return INT2NUM(xencoding);
}

Input.encoding_to_rb_encoding(Input::ENCODING) ⇒ Encoding (mod_func)

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

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_encoding.c', line 160

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" (mod_func)

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

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_encoding.c', line 67

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);
}