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



23
24
25
26
# File 'lib/libxml/reader.rb', line 23

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.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/libxml/ruby_xml_reader.c', line 89

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 (|).


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'ext/libxml/ruby_xml_reader.c', line 121

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 (|).


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
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/libxml/ruby_xml_reader.c', line 173

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 (|).


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
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'ext/libxml/ruby_xml_reader.c', line 236

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:



18
19
20
21
# File 'lib/libxml/reader.rb', line 18

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.



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'ext/libxml/ruby_xml_reader.c', line 834

static VALUE rxml_reader_attribute(VALUE self, VALUE key)
{
  VALUE result = Qnil;
  xmlChar *xattr;
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  if (TYPE(key) == T_FIXNUM)
  {
    xattr = xmlTextReaderGetAttributeNo(xReader, FIX2INT(key));
  }
  else
  {
    xattr = xmlTextReaderGetAttribute(xReader, (const xmlChar *) StringValueCStr(key));
  }

  if (xattr)
  {
    result = rxml_new_cstr(xattr, xencoding);
    xmlFree(xattr);
  }
  return result;
}

#attribute_countObject

Provide the number of attributes of the current node.



639
640
641
642
643
# File 'ext/libxml/ruby_xml_reader.c', line 639

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

#base_uriObject

Determine the base URI of the node.



677
678
679
680
681
682
683
684
# File 'ext/libxml/ruby_xml_reader.c', line 677

static VALUE rxml_reader_base_uri(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstBaseUri(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#byte_consumedObject

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



923
924
925
926
927
928
# File 'ext/libxml/ruby_xml_reader.c', line 923

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

#closeObject

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



282
283
284
285
286
# File 'ext/libxml/ruby_xml_reader.c', line 282

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

#column_numberNumeric

Provide the column number of the current parsing point.



938
939
940
941
942
943
# File 'ext/libxml/ruby_xml_reader.c', line 938

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

#default?Boolean

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



966
967
968
969
970
# File 'ext/libxml/ruby_xml_reader.c', line 966

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

#depthObject

Get the depth of the node in the tree.



737
738
739
740
741
# File 'ext/libxml/ruby_xml_reader.c', line 737

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

#empty_element?Boolean

Check if the current node is empty.



991
992
993
994
995
# File 'ext/libxml/ruby_xml_reader.c', line 991

static VALUE rxml_reader_empty_element(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  return xmlTextReaderIsEmptyElement(xreader) ? 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.



661
662
663
664
665
666
667
668
669
# File 'ext/libxml/ruby_xml_reader.c', line 661

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

Returns the current node and its full subtree. Note the returned node is valid ONLY until the next read call.



887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'ext/libxml/ruby_xml_reader.c', line 887

static VALUE rxml_reader_expand(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  xmlNodePtr xnode = NULL;

  /* At this point we need to wrap the reader's document as explained above. */
  xmlDocPtr xdoc = xmlTextReaderCurrentDoc(xreader);

  if (!xdoc)
    rb_raise(rb_eRuntimeError, "The reader does not have a document.  Did you forget to call read?");

  rxml_document_wrap(xdoc);

  /* And now hook in a mark function */
  RDATA(self)->dmark = (RUBY_DATA_FUNC)rxml_reader_mark;

  xnode = xmlTextReaderExpand(xreader);
  
  if (!xnode)
  {
    return Qnil;
  }
  else
  {
    return rxml_node_wrap(xnode);
  }
}

#has_attributes?Boolean

Get whether the node has attributes.



808
809
810
811
812
# File 'ext/libxml/ruby_xml_reader.c', line 808

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

#has_value?Boolean

Get whether the node can have a text value.



820
821
822
823
824
# File 'ext/libxml/ruby_xml_reader.c', line 820

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

#line_numberNumeric

Provide the line number of the current parsing point.



951
952
953
954
955
956
# File 'ext/libxml/ruby_xml_reader.c', line 951

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

#local_nameObject

Return the local name of the node.



624
625
626
627
628
629
630
631
# File 'ext/libxml/ruby_xml_reader.c', line 624

static VALUE rxml_reader_local_name(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstLocalName(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#lookup_namespace(prefix) ⇒ Object

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



865
866
867
868
869
870
871
872
873
874
875
876
877
878
# File 'ext/libxml/ruby_xml_reader.c', line 865

static VALUE rxml_reader_lookup_namespace(VALUE self, VALUE prefix)
{
  VALUE result = Qnil;
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *xnamespace = xmlTextReaderLookupNamespace(xReader, (const xmlChar *) StringValueCStr(prefix));
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  if (xnamespace)
  {
    result = rxml_new_cstr((const char*)xnamespace, (const char*)xencoding);
    xmlFree((void *)xnamespace);
  }
  return 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.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/libxml/ruby_xml_reader.c', line 296

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.



349
350
351
352
353
# File 'ext/libxml/ruby_xml_reader.c', line 349

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

#move_to_first_attributeObject

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



323
324
325
326
327
# File 'ext/libxml/ruby_xml_reader.c', line 323

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

#move_to_next_attributeObject

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



336
337
338
339
340
# File 'ext/libxml/ruby_xml_reader.c', line 336

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

#nameObject

Return the qualified name of the node.



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

static VALUE rxml_reader_name(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstName(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#namespace_declaration?Boolean

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



979
980
981
982
983
# File 'ext/libxml/ruby_xml_reader.c', line 979

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

#namespace_uriObject

Determine the namespace URI of the node.



692
693
694
695
696
697
698
699
# File 'ext/libxml/ruby_xml_reader.c', line 692

static VALUE rxml_reader_namespace_uri(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstNamespaceUri(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#nextObject

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



362
363
364
365
366
# File 'ext/libxml/ruby_xml_reader.c', line 362

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

#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.



376
377
378
379
380
# File 'ext/libxml/ruby_xml_reader.c', line 376

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

#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.



391
392
393
394
395
396
# File 'ext/libxml/ruby_xml_reader.c', line 391

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



405
406
407
408
409
# File 'ext/libxml/ruby_xml_reader.c', line 405

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

#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.



423
424
425
426
427
# File 'ext/libxml/ruby_xml_reader.c', line 423

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

#prefixObject

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



722
723
724
725
726
727
728
729
# File 'ext/libxml/ruby_xml_reader.c', line 722

static VALUE rxml_reader_prefix(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstPrefix(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#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).



750
751
752
753
754
# File 'ext/libxml/ruby_xml_reader.c', line 750

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

#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.



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'ext/libxml/ruby_xml_reader.c', line 437

static VALUE rxml_reader_read(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  int result = xmlTextReaderRead(xreader);
  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.



467
468
469
470
471
# File 'ext/libxml/ruby_xml_reader.c', line 467

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

#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.



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'ext/libxml/ruby_xml_reader.c', line 482

static VALUE rxml_reader_read_inner_xml(VALUE self)
{
  VALUE result = Qnil;
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);

  xmlChar *xml = xmlTextReaderReadInnerXml(xReader);

  if (xml)
  {
    const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);
    result = rxml_new_cstr((const char*) xml, xencoding);
    xmlFree(xml);
  }

  return 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.



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'ext/libxml/ruby_xml_reader.c', line 508

static VALUE rxml_reader_read_outer_xml(VALUE self)
{
  VALUE result = Qnil;
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);

  xmlChar *xml = xmlTextReaderReadOuterXml(xReader);

  if (xml)
  {
    const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);
    result = rxml_new_cstr((const char*) xml, xencoding);
    xmlFree(xml);
  }

  return result;
}

#read_stateObject

Get the read state of the reader.



531
532
533
534
535
# File 'ext/libxml/ruby_xml_reader.c', line 531

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

#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.



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/libxml/ruby_xml_reader.c', line 546

static VALUE rxml_reader_read_string(VALUE self)
{
  VALUE result = Qnil;
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);

  xmlChar *xml = xmlTextReaderReadString(xReader);

  if (xml)
  {
    const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);
    result = rxml_new_cstr((const char*) xml, xencoding);
    xmlFree(xml);
  }

  return 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.



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

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

#reset_error_handlerObject



6
7
8
9
# File 'lib/libxml/reader.rb', line 6

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.



593
594
595
596
597
598
599
600
# File 'ext/libxml/ruby_xml_reader.c', line 593

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

#set_error_handler(&block) ⇒ Object



11
12
13
14
# File 'lib/libxml/reader.rb', line 11

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.



766
767
768
769
770
# File 'ext/libxml/ruby_xml_reader.c', line 766

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

#valid?Boolean

Retrieve the validity status from the parser context.



1003
1004
1005
1006
1007
# File 'ext/libxml/ruby_xml_reader.c', line 1003

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

#valueObject

Provide the text value of the node if present.



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

static VALUE rxml_reader_value(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstValue(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#xml_langObject

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



778
779
780
781
782
783
784
785
# File 'ext/libxml/ruby_xml_reader.c', line 778

static VALUE rxml_reader_xml_lang(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstXmlLang(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}

#xml_versionObject

Determine the XML version of the document being read.



793
794
795
796
797
798
799
800
# File 'ext/libxml/ruby_xml_reader.c', line 793

static VALUE rxml_reader_xml_version(VALUE self)
{
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);
  const xmlChar *result = xmlTextReaderConstXmlVersion(xReader);
  const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);

  return (result == NULL ? Qnil : rxml_new_cstr(result, xencoding));
}