Class: LibXML::XML::Reader

Inherits:
Object
  • Object
show all
Defined in:
ext/libxml/ruby_xml_reader.c,
lib/libxml/reader.rb,
ext/libxml/ruby_xml_reader.c

Overview

The XML::Reader class provides a simpler, alternative way of parsing an XML document in contrast to XML::Parser or XML::SaxParser. A XML::Reader instance acts like a cursor going forward in a document stream, stopping at each node it encounters. To advance to the next node, simply cadd XML::Reader#read.

The XML::Reader API closely matches the DOM Core specification and supports namespaces, xml:base, entity handling and DTDs.

To summarize, XML::Reader provides a far simpler API to use versus XML::SaxParser and is more memory efficient than using XML::Parser to create a DOM tree.

Example:

parser = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
reader.read
assert_equal('foo', reader.name)
assert_equal(nil, reader.value)

3.times do |i|
  reader.read
  assert_equal(XML::Reader::TYPE_ELEMENT, reader.node_type)
  assert_equal('bar', reader.name)
  reader.read
  assert_equal(XML::Reader::TYPE_TEXT, reader.node_type)
  assert_equal((i + 1).to_s, reader.value)
  reader.read
  assert_equal(XML::Reader::TYPE_END_ELEMENT, reader.node_type)
end

You can also parse documents (see XML::Reader.document), strings (see XML::Parser.string) and io objects (see XML::Parser.io).

For a more in depth tutorial, albeit in C, see xmlsoft.org/xmlreader.html.

Constant Summary collapse

LOADDTD =

Constants

INT2FIX(XML_PARSER_LOADDTD)
DEFAULTATTRS =
INT2FIX(XML_PARSER_DEFAULTATTRS)
VALIDATE =
INT2FIX(XML_PARSER_VALIDATE)
SUBST_ENTITIES =
INT2FIX(XML_PARSER_SUBST_ENTITIES)
SEVERITY_VALIDITY_WARNING =
INT2FIX(XML_PARSER_SEVERITY_VALIDITY_WARNING)
SEVERITY_VALIDITY_ERROR =
INT2FIX(XML_PARSER_SEVERITY_VALIDITY_ERROR)
SEVERITY_WARNING =
INT2FIX(XML_PARSER_SEVERITY_WARNING)
SEVERITY_ERROR =
INT2FIX(XML_PARSER_SEVERITY_ERROR)
TYPE_NONE =
INT2FIX(XML_READER_TYPE_NONE)
TYPE_ELEMENT =
INT2FIX(XML_READER_TYPE_ELEMENT)
TYPE_ATTRIBUTE =
INT2FIX(XML_READER_TYPE_ATTRIBUTE)
TYPE_TEXT =
INT2FIX(XML_READER_TYPE_TEXT)
TYPE_CDATA =
INT2FIX(XML_READER_TYPE_CDATA)
TYPE_ENTITY_REFERENCE =
INT2FIX(XML_READER_TYPE_ENTITY_REFERENCE)
TYPE_ENTITY =
INT2FIX(XML_READER_TYPE_ENTITY)
TYPE_PROCESSING_INSTRUCTION =
INT2FIX(XML_READER_TYPE_PROCESSING_INSTRUCTION)
TYPE_COMMENT =
INT2FIX(XML_READER_TYPE_COMMENT)
TYPE_DOCUMENT =
INT2FIX(XML_READER_TYPE_DOCUMENT)
TYPE_DOCUMENT_TYPE =
INT2FIX(XML_READER_TYPE_DOCUMENT_TYPE)
TYPE_DOCUMENT_FRAGMENT =
INT2FIX(XML_READER_TYPE_DOCUMENT_FRAGMENT)
TYPE_NOTATION =
INT2FIX(XML_READER_TYPE_NOTATION)
TYPE_WHITESPACE =
INT2FIX(XML_READER_TYPE_WHITESPACE)
TYPE_SIGNIFICANT_WHITESPACE =
INT2FIX(XML_READER_TYPE_SIGNIFICANT_WHITESPACE)
TYPE_END_ELEMENT =
INT2FIX(XML_READER_TYPE_END_ELEMENT)
TYPE_END_ENTITY =
INT2FIX(XML_READER_TYPE_END_ENTITY)
TYPE_XML_DECLARATION =
INT2FIX(XML_READER_TYPE_XML_DECLARATION)
MODE_INITIAL =

Read states

INT2FIX(XML_TEXTREADER_MODE_INITIAL)
MODE_INTERACTIVE =
INT2FIX(XML_TEXTREADER_MODE_INTERACTIVE)
MODE_ERROR =
INT2FIX(XML_TEXTREADER_MODE_ERROR)
MODE_EOF =
INT2FIX(XML_TEXTREADER_MODE_EOF)
MODE_CLOSED =
INT2FIX(XML_TEXTREADER_MODE_CLOSED)
MODE_READING =
INT2FIX(XML_TEXTREADER_MODE_READING)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.data(string, options = nil) ⇒ Object



21
22
23
24
# File 'lib/libxml/reader.rb', line 21

def self.data(string, options = nil)
  warn("XML::Reader.data is deprecated.  Use XML::Reader.string instead")
  self.string(string, options)
end

.XML::Reader.document(doc) ⇒ XML::Reader

Create an new reader for the specified document.

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/libxml/ruby_xml_reader.c', line 76

VALUE rxml_reader_document(VALUE klass, VALUE doc)
{
  xmlDocPtr xdoc;
  xmlTextReaderPtr xreader;

  Data_Get_Struct(doc, xmlDoc, xdoc);

  xreader = xmlReaderWalker(xdoc);

  if (xreader == NULL)
    rxml_raise(&xmlLastError);

  return rxml_reader_wrap(xreader);
}

.XML::Reader.file(path) ⇒ XML::Reader .XML::Reader.file(path, : encoding) ⇒ XML::Encoding::UTF_8

Creates a new reader by parsing the specified file or uri.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

encoding - The document encoding, defaults to nil. Valid values
           are the encoding constants defined on XML::Encoding.
options - Controls the execution of the parser, defaults to 0.
          Valid values are the constants defined on
          XML::Parser::Options.  Mutliple options can be combined
          by using Bitwise OR (|).

Overloads:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/libxml/ruby_xml_reader.c', line 108

static VALUE rxml_reader_file(int argc, VALUE *argv, VALUE klass)
{
  xmlTextReaderPtr xreader;
  VALUE path;
  VALUE options;

  const char *xencoding = NULL;
  int xoptions = 0;

  rb_scan_args(argc, argv, "11", &path, &options);
  Check_Type(path, T_STRING);

  if (!NIL_P(options))
  {
    VALUE encoding = Qnil;
    VALUE parserOptions = Qnil;

    Check_Type(options, T_HASH);

    encoding = rb_hash_aref(options, BASE_URI_SYMBOL);
    xencoding = NIL_P(encoding) ? NULL : xmlGetCharEncodingName(NUM2INT(encoding));

    parserOptions = rb_hash_aref(options, OPTIONS_SYMBOL);
    xoptions = NIL_P(parserOptions) ? 0 : NUM2INT(parserOptions);
  }

  xreader = xmlReaderForFile(StringValueCStr(path), xencoding, xoptions);

  if (xreader == NULL)
    rxml_raise(&xmlLastError);

  return rxml_reader_wrap(xreader);
}

.XML::Reader.io(io) ⇒ XML::Reader .XML::Reader.io(io, : encoding) ⇒ XML::Encoding::UTF_8

Creates a new reader by parsing the specified io object.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
           are the encoding constants defined on XML::Encoding.
options - Controls the execution of the parser, defaults to 0.
          Valid values are the constants defined on
          XML::Parser::Options.  Mutliple options can be combined
          by using Bitwise OR (|).

Overloads:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'ext/libxml/ruby_xml_reader.c', line 160

static VALUE rxml_reader_io(int argc, VALUE *argv, VALUE klass)
{
  xmlTextReaderPtr xreader;
  VALUE result;
  VALUE io;
  VALUE options;
  char *xbaseurl = NULL;
  const char *xencoding = NULL;
  int xoptions = 0;

  rb_scan_args(argc, argv, "11", &io, &options);

  if (!NIL_P(options))
  {
    VALUE baseurl = Qnil;
    VALUE encoding = Qnil;
    VALUE parserOptions = Qnil;

    Check_Type(options, T_HASH);

    baseurl = rb_hash_aref(options, BASE_URI_SYMBOL);
    xbaseurl = NIL_P(baseurl) ? NULL : StringValueCStr(baseurl);

    encoding = rb_hash_aref(options, ENCODING_SYMBOL);
    xencoding = NIL_P(encoding) ? NULL : xmlGetCharEncodingName(NUM2INT(encoding));

    parserOptions = rb_hash_aref(options, OPTIONS_SYMBOL);
    xoptions = NIL_P(parserOptions) ? 0 : NUM2INT(parserOptions);
  }
  
  xreader = xmlReaderForIO((xmlInputReadCallback) rxml_read_callback, NULL,
                           (void *) io, 
                           xbaseurl, xencoding, xoptions);

  if (xreader == NULL)
    rxml_raise(&xmlLastError);

  result = rxml_reader_wrap(xreader);

  /* Attach io object to parser so it won't get freed.*/
  rb_ivar_set(result, IO_ATTR, io);

  return result;
}

.XML::Reader.string(io) ⇒ XML::Reader .XML::Reader.string(io, : encoding) ⇒ XML::Encoding::UTF_8

Creates a new reader by parsing the specified string.

You may provide an optional hash table to control how the parsing is performed. Valid options are:

base_uri - The base url for the parsed document.
encoding - The document encoding, defaults to nil. Valid values
           are the encoding constants defined on XML::Encoding.
options - Controls the execution of the parser, defaults to 0.
          Valid values are the constants defined on
          XML::Parser::Options.  Mutliple options can be combined
          by using Bitwise OR (|).

Overloads:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'ext/libxml/ruby_xml_reader.c', line 223

static VALUE rxml_reader_string(int argc, VALUE *argv, VALUE klass)
{
  xmlTextReaderPtr xreader;
  VALUE string;
  VALUE options;
  char *xbaseurl = NULL;
  const char *xencoding = NULL;
  int xoptions = 0;

  rb_scan_args(argc, argv, "11", &string, &options);
  Check_Type(string, T_STRING);

  if (!NIL_P(options))
  {
    VALUE baseurl = Qnil;
    VALUE encoding = Qnil;
    VALUE parserOptions = Qnil;

    Check_Type(options, T_HASH);

    baseurl = rb_hash_aref(options, BASE_URI_SYMBOL);
    xbaseurl = NIL_P(baseurl) ? NULL : StringValueCStr(baseurl);

    encoding = rb_hash_aref(options, ENCODING_SYMBOL);
    xencoding = NIL_P(encoding) ? NULL : xmlGetCharEncodingName(NUM2INT(encoding));
      
    parserOptions = rb_hash_aref(options, OPTIONS_SYMBOL);
    xoptions = NIL_P(parserOptions) ? 0 : NUM2INT(parserOptions);
  }
  
  xreader = xmlReaderForMemory(StringValueCStr(string), RSTRING_LEN(string), 
                               xbaseurl, xencoding, xoptions);

  if (xreader == NULL)
    rxml_raise(&xmlLastError);

  return rxml_reader_wrap(xreader);
}

.walker(doc) ⇒ Object

:enddoc:



16
17
18
19
# File 'lib/libxml/reader.rb', line 16

def self.walker(doc)
  warn("XML::Reader.walker is deprecated.  Use XML::Reader.document instead")
  self.document(doc)
end

Instance Method Details

#[](key) ⇒ Object

Provide the value of the attribute with the specified index (if key is an integer) or with the specified name (if key is a string) relative to the containing element, as a string.



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'ext/libxml/ruby_xml_reader.c', line 746

static VALUE rxml_reader_attribute(VALUE self, VALUE key)
{
  xmlTextReaderPtr reader;
  xmlChar *attr;

  reader = rxml_text_reader_get(self);

  if (TYPE(key) == T_FIXNUM)
  {
    attr = xmlTextReaderGetAttributeNo(reader, FIX2INT(key));
  }
  else
  {
    attr = xmlTextReaderGetAttribute(reader, (const xmlChar *) StringValueCStr(key));
  }
  return (attr == NULL ? Qnil : rb_str_new2((const char*)attr));
}

#attribute_countObject

Provide the number of attributes of the current node.



574
575
576
577
# File 'ext/libxml/ruby_xml_reader.c', line 574

static VALUE rxml_reader_attr_count(VALUE self)
{
  return INT2FIX(xmlTextReaderAttributeCount(rxml_text_reader_get(self)));
}

#base_uriObject

Determine the base URI of the node.



611
612
613
614
615
# File 'ext/libxml/ruby_xml_reader.c', line 611

static VALUE rxml_reader_base_uri(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstBaseUri(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#byte_consumedObject

This method provides the current index of the parser used by the reader, relative to the start of the current entity.



818
819
820
821
822
# File 'ext/libxml/ruby_xml_reader.c', line 818

static VALUE
rxml_reader_byte_consumed(VALUE self)
{
  return INT2NUM(xmlTextReaderByteConsumed(rxml_text_reader_get(self)));
}

#closeObject

This method releases any resources allocated by the current instance changes the state to Closed and close any underlying input.



269
270
271
272
# File 'ext/libxml/ruby_xml_reader.c', line 269

static VALUE rxml_reader_close(VALUE self)
{
  return INT2FIX(xmlTextReaderClose(rxml_text_reader_get(self)));
}

#column_numberNumeric

Provide the column number of the current parsing point.

Returns:

  • (Numeric)


832
833
834
835
836
# File 'ext/libxml/ruby_xml_reader.c', line 832

static VALUE
rxml_reader_column_number(VALUE self)
{
  return INT2NUM(xmlTextReaderGetParserColumnNumber(rxml_text_reader_get(self)));
}

#default?Boolean

Return whether an Attribute node was generated from the default value defined in the DTD or schema.

Returns:

  • (Boolean)


858
859
860
861
# File 'ext/libxml/ruby_xml_reader.c', line 858

static VALUE rxml_reader_default(VALUE self)
{
  return xmlTextReaderIsDefault(rxml_text_reader_get(self)) ? Qtrue : Qfalse;
}

#depthObject

Get the depth of the node in the tree.



659
660
661
662
# File 'ext/libxml/ruby_xml_reader.c', line 659

static VALUE rxml_reader_depth(VALUE self)
{
  return INT2FIX(xmlTextReaderDepth(rxml_text_reader_get(self)));
}

#empty_element?Boolean

Check if the current node is empty.

Returns:

  • (Boolean)


882
883
884
885
886
# File 'ext/libxml/ruby_xml_reader.c', line 882

static VALUE rxml_reader_empty_element(VALUE self)
{
  return xmlTextReaderIsEmptyElement(rxml_text_reader_get(self)) ? Qtrue
      : Qfalse;
}

#encodingXML::Encoding::UTF_8

Returns the encoding of the document being read. Note you first have to read data from the reader for encoding to return a value

reader = XML::Reader.file(XML_FILE)
assert_nil(reader.encoding)
reader.read
assert_equal(XML::Encoding::UTF_8, reader.encoding)

In addition, libxml always appears to return nil for the encoding when parsing strings.



595
596
597
598
599
600
601
602
603
# File 'ext/libxml/ruby_xml_reader.c', line 595

static VALUE rxml_reader_encoding(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xreader);
  if (xencoding)
    return INT2NUM(xmlParseCharEncoding(xencoding));
  else
    return INT2NUM(XML_CHAR_ENCODING_NONE);
}

#expandObject

Read the contents of the current node and the full subtree. It then makes the subtree available until the next read call.

Return an XML::Node object, or nil in case of error.



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
# File 'ext/libxml/ruby_xml_reader.c', line 787

static VALUE rxml_reader_expand(VALUE self)
{
  xmlNodePtr node;
  xmlDocPtr doc;
  xmlTextReaderPtr reader = rxml_text_reader_get(self);
  node = xmlTextReaderExpand(reader);

  if (!node)
    return Qnil;

  /* Okay this is tricky.  By accessing the returned node, we
   take ownership of the reader's document.  Thus we need to
   tell the reader to not free it.  Otherwise it will be
   freed twice - once when the Ruby document wrapper goes
   out of scope and once when the reader goes out of scope. */

  xmlTextReaderPreserve(reader);
  doc = xmlTextReaderCurrentDoc(reader);
  rxml_document_wrap(doc);

  return rxml_node_wrap(node);
}

#has_attributes?Boolean

Get whether the node has attributes.

Returns:

  • (Boolean)


721
722
723
724
725
# File 'ext/libxml/ruby_xml_reader.c', line 721

static VALUE rxml_reader_has_attributes(VALUE self)
{
  return xmlTextReaderHasAttributes(rxml_text_reader_get(self)) ? Qtrue
      : Qfalse;
}

#has_value?Boolean

Get whether the node can have a text value.

Returns:

  • (Boolean)


733
734
735
736
# File 'ext/libxml/ruby_xml_reader.c', line 733

static VALUE rxml_reader_has_value(VALUE self)
{
  return xmlTextReaderHasValue(rxml_text_reader_get(self)) ? Qtrue : Qfalse;
}

#line_numberNumeric

Provide the line number of the current parsing point.

Returns:

  • (Numeric)


844
845
846
847
848
# File 'ext/libxml/ruby_xml_reader.c', line 844

static VALUE
rxml_reader_line_number(VALUE self)
{
  return INT2NUM(xmlTextReaderGetParserLineNumber(rxml_text_reader_get(self)));
}

#local_nameObject

Return the local name of the node.



562
563
564
565
566
# File 'ext/libxml/ruby_xml_reader.c', line 562

static VALUE rxml_reader_local_name(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstLocalName(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#lookup_namespace(prefix) ⇒ Object

Resolve a namespace prefix in the scope of the current element. To return the default namespace, specify nil as prefix.



771
772
773
774
775
776
# File 'ext/libxml/ruby_xml_reader.c', line 771

static VALUE rxml_reader_lookup_namespace(VALUE self, VALUE prefix)
{
  const xmlChar *result = xmlTextReaderLookupNamespace(rxml_text_reader_get(
      self), (const xmlChar *) StringValueCStr(prefix));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#move_to_attribute(val) ⇒ Object

Move the position of the current instance to the attribute with the specified index (if val is an integer) or name (if val is a string) relative to the containing element.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'ext/libxml/ruby_xml_reader.c', line 282

static VALUE rxml_reader_move_to_attr(VALUE self, VALUE val)
{
  xmlTextReaderPtr xreader;
  int ret;

  xreader = rxml_text_reader_get(self);

  if (TYPE(val) == T_FIXNUM)
  {
    ret = xmlTextReaderMoveToAttributeNo(xreader, FIX2INT(val));
  }
  else
  {
    ret = xmlTextReaderMoveToAttribute(xreader,
        (const xmlChar *) StringValueCStr(val));
  }

  return INT2FIX(ret);
}

#move_to_elementObject

Move the position of the current instance to the node that contains the current attribute node.



333
334
335
336
# File 'ext/libxml/ruby_xml_reader.c', line 333

static VALUE rxml_reader_move_to_element(VALUE self)
{
  return INT2FIX(xmlTextReaderMoveToElement(rxml_text_reader_get(self)));
}

#move_to_first_attributeObject

Move the position of the current instance to the first attribute associated with the current node.



309
310
311
312
# File 'ext/libxml/ruby_xml_reader.c', line 309

static VALUE rxml_reader_move_to_first_attr(VALUE self)
{
  return INT2FIX(xmlTextReaderMoveToFirstAttribute(rxml_text_reader_get(self)));
}

#move_to_next_attributeObject

Move the position of the current instance to the next attribute associated with the current node.



321
322
323
324
# File 'ext/libxml/ruby_xml_reader.c', line 321

static VALUE rxml_reader_move_to_next_attr(VALUE self)
{
  return INT2FIX(xmlTextReaderMoveToNextAttribute(rxml_text_reader_get(self)));
}

#nameObject

Return the qualified name of the node.



550
551
552
553
554
# File 'ext/libxml/ruby_xml_reader.c', line 550

static VALUE rxml_reader_name(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstName(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#namespace_declaration?Boolean

Determine whether the current node is a namespace declaration rather than a regular attribute.

Returns:

  • (Boolean)


870
871
872
873
874
# File 'ext/libxml/ruby_xml_reader.c', line 870

static VALUE rxml_reader_namespace_declaration(VALUE self)
{
  return xmlTextReaderIsNamespaceDecl(rxml_text_reader_get(self)) ? Qtrue
      : Qfalse;
}

#namespace_uriObject

Determine the namespace URI of the node.



623
624
625
626
627
# File 'ext/libxml/ruby_xml_reader.c', line 623

static VALUE rxml_reader_namespace_uri(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstNamespaceUri(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#nextObject

Skip to the node following the current one in document order while avoiding the subtree if any.



345
346
347
348
# File 'ext/libxml/ruby_xml_reader.c', line 345

static VALUE rxml_reader_next(VALUE self)
{
  return INT2FIX(xmlTextReaderNext(rxml_text_reader_get(self)));
}

#next_siblingObject

Skip to the node following the current one in document order while avoiding the subtree if any. Currently implemented only for Readers built on a document.



358
359
360
361
# File 'ext/libxml/ruby_xml_reader.c', line 358

static VALUE rxml_reader_next_sibling(VALUE self)
{
  return INT2FIX(xmlTextReaderNextSibling(rxml_text_reader_get(self)));
}

#nodeXML::Node

Returns the reader’s current node. It will return nil if Reader#read has not yet been called. WARNING - Using this method is dangerous because the the node may be destroyed on the next #read.

Returns:



372
373
374
375
376
377
# File 'ext/libxml/ruby_xml_reader.c', line 372

static VALUE rxml_reader_node(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  xmlNodePtr xnode = xmlTextReaderCurrentNode(xreader);
  return xnode ? rxml_node_wrap(xnode) : Qnil;
}

#node_typeObject

Get the node type of the current node. Reference: dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html



386
387
388
389
# File 'ext/libxml/ruby_xml_reader.c', line 386

static VALUE rxml_reader_node_type(VALUE self)
{
  return INT2FIX(xmlTextReaderNodeType(rxml_text_reader_get(self)));
}

#normalizationObject

The value indicating whether to normalize white space and attribute values. Since attribute value and end of line normalizations are a MUST in the XML specification only the value true is accepted. The broken bahaviour of accepting out of range character entities like &#0; is of course not supported either.

Return 1 or -1 in case of error.



403
404
405
406
# File 'ext/libxml/ruby_xml_reader.c', line 403

static VALUE rxml_reader_normalization(VALUE self)
{
  return INT2FIX(xmlTextReaderNormalization(rxml_text_reader_get(self)));
}

#prefixObject

Get a shorthand reference to the namespace associated with the node.



647
648
649
650
651
# File 'ext/libxml/ruby_xml_reader.c', line 647

static VALUE rxml_reader_prefix(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstPrefix(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#quote_charString

Get the quotation mark character used to enclose the value of an attribute, as an integer value (and -1 in case of error).

Returns:

  • (String)


671
672
673
674
# File 'ext/libxml/ruby_xml_reader.c', line 671

static VALUE rxml_reader_quote_char(VALUE self)
{
  return INT2FIX(xmlTextReaderQuoteChar(rxml_text_reader_get(self)));
}

#readObject

Causes the reader to move to the next node in the stream, exposing its properties.

Returns true if a node was successfully read or false if there are no more nodes to read. On errors, an exception is raised.



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'ext/libxml/ruby_xml_reader.c', line 416

static VALUE rxml_reader_read(VALUE self)
{
  int result = xmlTextReaderRead(rxml_text_reader_get(self));
  switch(result)
  {
    case -1:
      rxml_raise(&xmlLastError);
      return Qnil;
      break;
    case 0:
      return Qfalse;
    case 1:
      return Qtrue;
    default:
      rb_raise(rb_eRuntimeError,
               "xmlTextReaderRead did not return -1, 0 or 1.  Return value was: %d", result);
  }
}

#read_attribute_valueObject

Parse an attribute value into one or more Text and EntityReference nodes.

Return 1 in case of success, 0 if the reader was not positionned on an attribute node or all the attribute values have been read, or -1 in case of error.



445
446
447
448
# File 'ext/libxml/ruby_xml_reader.c', line 445

static VALUE rxml_reader_read_attr_value(VALUE self)
{
  return INT2FIX(xmlTextReaderReadAttributeValue(rxml_text_reader_get(self)));
}

#read_inner_xmlObject

Read the contents of the current node, including child nodes and markup.

Return a string containing the XML content, or nil if the current node is neither an element nor attribute, or has no child nodes.



459
460
461
462
463
# File 'ext/libxml/ruby_xml_reader.c', line 459

static VALUE rxml_reader_read_inner_xml(VALUE self)
{
  const xmlChar *result = xmlTextReaderReadInnerXml(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#read_outer_xmlObject

Read the contents of the current node, including child nodes and markup.

Return a string containing the XML content, or nil if the current node is neither an element nor attribute, or has no child nodes.



474
475
476
477
478
# File 'ext/libxml/ruby_xml_reader.c', line 474

static VALUE rxml_reader_read_outer_xml(VALUE self)
{
  const xmlChar *result = xmlTextReaderReadOuterXml(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#read_stateObject

Get the read state of the reader.



486
487
488
489
# File 'ext/libxml/ruby_xml_reader.c', line 486

static VALUE rxml_reader_read_state(VALUE self)
{
  return INT2FIX(xmlTextReaderReadState(rxml_text_reader_get(self)));
}

#read_stringString

Read the contents of an element or a text node as a string.

Return a string containing the contents of the Element or Text node, or nil if the reader is positioned on any other type of node.

Returns:

  • (String)


500
501
502
503
504
# File 'ext/libxml/ruby_xml_reader.c', line 500

static VALUE rxml_reader_read_string(VALUE self)
{
  const xmlChar *result = xmlTextReaderReadString(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#relax_ng_validate(rng) ⇒ Object

Use RelaxNG to validate the document as it is processed. Activation is only possible before the first read. If rng is nil, the RelaxNG validation is desactivated.

Return 0 in case the RelaxNG validation could be (des)activated and -1 in case of error.



517
518
519
520
521
# File 'ext/libxml/ruby_xml_reader.c', line 517

static VALUE rxml_reader_relax_ng_validate(VALUE self, VALUE rng)
{
  char *xrng = NIL_P(rng) ? NULL : StringValueCStr(rng);
  return INT2FIX(xmlTextReaderRelaxNGValidate(rxml_text_reader_get(self), xrng));
}

#reset_error_handlerObject



4
5
6
7
# File 'lib/libxml/reader.rb', line 4

def reset_error_handler
  warn('reset_error_handler is deprecated.  Use Error.reset_handler instead')
  Error.reset_handler
end

#schema_validate(schema) ⇒ Object

Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first read. If schema is nil, then XML Schema validation is desactivated.

Return 0 in case the schemas validation could be (de)activated and -1 in case of error.



535
536
537
538
539
540
541
# File 'ext/libxml/ruby_xml_reader.c', line 535

static VALUE
rxml_reader_schema_validate(VALUE self, VALUE xsd)
{
  char *xxsd = NIL_P(xsd) ? NULL : StringValueCStr(xsd);
  int status = xmlTextReaderSchemaValidate(rxml_text_reader_get(self), xxsd);
  return INT2FIX(status);
}

#set_error_handler(&block) ⇒ Object



9
10
11
12
# File 'lib/libxml/reader.rb', line 9

def set_error_handler(&block)
  warn('set_error_handler is deprecated.  Use Error.set_handler instead')
  Error.set_handler(&block)
end

#standaloneObject

Determine the standalone status of the document being read.

Return 1 if the document was declared to be standalone, 0 if it was declared to be not standalone, or -1 if the document did not specify its standalone status or in case of error.



686
687
688
689
# File 'ext/libxml/ruby_xml_reader.c', line 686

static VALUE rxml_reader_standalone(VALUE self)
{
  return INT2FIX(xmlTextReaderStandalone(rxml_text_reader_get(self)));
}

#valid?Boolean

Retrieve the validity status from the parser context.

Returns:

  • (Boolean)


894
895
896
897
# File 'ext/libxml/ruby_xml_reader.c', line 894

static VALUE rxml_reader_valid(VALUE self)
{
  return xmlTextReaderIsValid(rxml_text_reader_get(self)) ? Qtrue : Qfalse;
}

#valueObject

Provide the text value of the node if present.



635
636
637
638
639
# File 'ext/libxml/ruby_xml_reader.c', line 635

static VALUE rxml_reader_value(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstValue(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#xml_langObject

Get the xml:lang scope within which the node resides.



697
698
699
700
701
# File 'ext/libxml/ruby_xml_reader.c', line 697

static VALUE rxml_reader_xml_lang(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstXmlLang(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}

#xml_versionObject

Determine the XML version of the document being read.



709
710
711
712
713
# File 'ext/libxml/ruby_xml_reader.c', line 709

static VALUE rxml_reader_xml_version(VALUE self)
{
  const xmlChar *result = xmlTextReaderConstXmlVersion(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}