Class: Nokogiri::XML::Reader
- Inherits:
-
Object
- Object
- Nokogiri::XML::Reader
- Includes:
- Enumerable
- Defined in:
- lib/nokogiri/xml/reader.rb,
ext/nokogiri/xml_reader.c
Overview
Nokogiri::XML::Reader parses an XML document similar to the way a cursor would move. The Reader is given an XML document, and yields nodes to an each block.
Here is an example of usage:
reader = Nokogiri::XML::Reader(<<-eoxml)
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
</x>
eoxml
reader.each do |node|
# node is an instance of Nokogiri::XML::Reader
puts node.name
end
Note that Nokogiri::XML::Reader#each can only be called once!! Once the cursor moves through the entire document, you must parse the document again. So make sure that you capture any information you need during the first iteration.
The Reader parser is good for when you need the speed of a SAX parser, but do not want to write a Document handler.
Constant Summary collapse
- TYPE_NONE =
0
- TYPE_ELEMENT =
Element node type
1
- TYPE_ATTRIBUTE =
Attribute node type
2
- TYPE_TEXT =
Text node type
3
- TYPE_CDATA =
CDATA node type
4
- TYPE_ENTITY_REFERENCE =
Entity Reference node type
5
- TYPE_ENTITY =
Entity node type
6
- TYPE_PROCESSING_INSTRUCTION =
PI node type
7
- TYPE_COMMENT =
Comment node type
8
- TYPE_DOCUMENT =
Document node type
9
- TYPE_DOCUMENT_TYPE =
Document Type node type
10
- TYPE_DOCUMENT_FRAGMENT =
Document Fragment node type
11
- TYPE_NOTATION =
Notation node type
12
- TYPE_WHITESPACE =
Whitespace node type
13
- TYPE_SIGNIFICANT_WHITESPACE =
Significant Whitespace node type
14
- TYPE_END_ELEMENT =
Element end node type
15
- TYPE_END_ENTITY =
Entity end node type
16
- TYPE_XML_DECLARATION =
XML Declaration node type
17
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
The encoding for the document.
-
#errors ⇒ Object
A list of errors encountered while parsing.
-
#source ⇒ Object
readonly
The XML source.
Class Method Summary collapse
-
.from_io(io, url = nil, encoding = nil, options = 0) ⇒ Object
Create a new reader that parses
io
. -
.from_memory(string, url = nil, encoding = nil, options = 0) ⇒ Object
Create a new reader that parses
string
.
Instance Method Summary collapse
-
#attribute(name) ⇒ Object
Get the value of attribute named
name
. -
#attribute_at(index) ⇒ Object
Get the value of attribute at
index
. -
#attribute_count ⇒ Object
Get the number of attributes for the current node.
-
#attribute_nodes ⇒ Object
Get a list of attributes for the current node.
-
#attributes ⇒ Object
Get a list of attributes for the current node.
-
#attributes? ⇒ Boolean
Does this node have attributes?.
-
#base_uri ⇒ Object
base_uri.
-
#default? ⇒ Boolean
Was an attribute generated from the default value in the DTD or schema?.
-
#depth ⇒ Object
Get the depth of the node.
-
#each ⇒ Object
Move the cursor through the document yielding the cursor to the block.
-
#empty_element?(#) ⇒ Boolean
(also: #self_closing?)
Returns true if the current node is empty, otherwise false.
-
#inner_xml ⇒ Object
Read the contents of the current node, including child nodes and markup.
-
#lang ⇒ Object
Get the xml:lang scope within which the node resides.
-
#local_name ⇒ Object
Get the local name of the node.
-
#name ⇒ Object
Get the name of the node.
-
#namespace_uri ⇒ Object
Get the URI defining the namespace associated with the node.
-
#namespaces ⇒ Object
Get a hash of namespaces for this Node.
-
#node_type ⇒ Object
Get the type of readers current node.
-
#outer_xml ⇒ Object
Read the current node and its contents, including child nodes and markup.
-
#prefix ⇒ Object
Get the shorthand reference to the namespace associated with the node.
-
#read ⇒ Object
Move the Reader forward through the XML document.
-
#state ⇒ Object
Get the state of the reader.
-
#value ⇒ Object
Get the text value of the node if present.
-
#value? ⇒ Boolean
Does this node have a text value?.
-
#xml_version ⇒ Object
Get the XML version of the document being read.
Instance Attribute Details
#encoding ⇒ Object (readonly)
The encoding for the document
74 75 76 |
# File 'lib/nokogiri/xml/reader.rb', line 74 def encoding @encoding end |
#errors ⇒ Object
A list of errors encountered while parsing
71 72 73 |
# File 'lib/nokogiri/xml/reader.rb', line 71 def errors @errors end |
#source ⇒ Object (readonly)
The XML source
77 78 79 |
# File 'lib/nokogiri/xml/reader.rb', line 77 def source @source end |
Class Method Details
.from_io(io, url = nil, encoding = nil, options = 0) ⇒ Object
Create a new reader that parses io
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'ext/nokogiri/xml_reader.c', line 557
static VALUE from_io(int argc, VALUE *argv, VALUE klass)
{
VALUE rb_io, rb_url, encoding, rb_options;
xmlTextReaderPtr reader;
const char * c_url = NULL;
const char * c_encoding = NULL;
int c_options = 0;
VALUE rb_reader, args[3];
rb_scan_args(argc, argv, "13", &rb_io, &rb_url, &encoding, &rb_options);
if (!RTEST(rb_io)) rb_raise(rb_eArgError, "io cannot be nil");
if (RTEST(rb_url)) c_url = StringValueCStr(rb_url);
if (RTEST(encoding)) c_encoding = StringValueCStr(encoding);
if (RTEST(rb_options)) c_options = (int)NUM2INT(rb_options);
reader = xmlReaderForIO(
(xmlInputReadCallback)io_read_callback,
(xmlInputCloseCallback)io_close_callback,
(void *)rb_io,
c_url,
c_encoding,
c_options
);
if(reader == NULL) {
xmlFreeTextReader(reader);
rb_raise(rb_eRuntimeError, "couldn't create a parser");
}
rb_reader = Data_Wrap_Struct(klass, NULL, dealloc, reader);
args[0] = rb_io;
args[1] = rb_url;
args[2] = encoding;
rb_obj_call_init(rb_reader, 3, args);
return rb_reader;
}
|
.from_memory(string, url = nil, encoding = nil, options = 0) ⇒ Object
Create a new reader that parses string
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'ext/nokogiri/xml_reader.c', line 513
static VALUE from_memory(int argc, VALUE *argv, VALUE klass)
{
VALUE rb_buffer, rb_url, encoding, rb_options;
xmlTextReaderPtr reader;
const char * c_url = NULL;
const char * c_encoding = NULL;
int c_options = 0;
VALUE rb_reader, args[3];
rb_scan_args(argc, argv, "13", &rb_buffer, &rb_url, &encoding, &rb_options);
if (!RTEST(rb_buffer)) rb_raise(rb_eArgError, "string cannot be nil");
if (RTEST(rb_url)) c_url = StringValueCStr(rb_url);
if (RTEST(encoding)) c_encoding = StringValueCStr(encoding);
if (RTEST(rb_options)) c_options = (int)NUM2INT(rb_options);
reader = xmlReaderForMemory(
StringValuePtr(rb_buffer),
(int)RSTRING_LEN(rb_buffer),
c_url,
c_encoding,
c_options
);
if(reader == NULL) {
xmlFreeTextReader(reader);
rb_raise(rb_eRuntimeError, "couldn't create a parser");
}
rb_reader = Data_Wrap_Struct(klass, NULL, dealloc, reader);
args[0] = rb_buffer;
args[1] = rb_url;
args[2] = encoding;
rb_obj_call_init(rb_reader, 3, args);
return rb_reader;
}
|
Instance Method Details
#attribute(name) ⇒ Object
Get the value of attribute named name
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'ext/nokogiri/xml_reader.c', line 199
static VALUE reader_attribute(VALUE self, VALUE name)
{
xmlTextReaderPtr reader;
xmlChar *value ;
VALUE rb_value;
Data_Get_Struct(self, xmlTextReader, reader);
if(NIL_P(name)) return Qnil;
name = StringValue(name) ;
value = xmlTextReaderGetAttribute(reader, (xmlChar*)StringValueCStr(name));
if(value == NULL) return Qnil;
rb_value = NOKOGIRI_STR_NEW2(value);
xmlFree(value);
return rb_value;
}
|
#attribute_at(index) ⇒ Object
Get the value of attribute at index
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/nokogiri/xml_reader.c', line 171
static VALUE attribute_at(VALUE self, VALUE index)
{
xmlTextReaderPtr reader;
xmlChar *value;
VALUE rb_value;
Data_Get_Struct(self, xmlTextReader, reader);
if(NIL_P(index)) return Qnil;
index = rb_Integer(index);
value = xmlTextReaderGetAttributeNo(
reader,
(int)NUM2INT(index)
);
if(value == NULL) return Qnil;
rb_value = NOKOGIRI_STR_NEW2(value);
xmlFree(value);
return rb_value;
}
|
#attribute_count ⇒ Object
Get the number of attributes for the current node
224 225 226 227 228 229 230 231 232 233 234 |
# File 'ext/nokogiri/xml_reader.c', line 224
static VALUE attribute_count(VALUE self)
{
xmlTextReaderPtr reader;
int count;
Data_Get_Struct(self, xmlTextReader, reader);
count = xmlTextReaderAttributeCount(reader);
if(count == -1) return Qnil;
return INT2NUM((long)count);
}
|
#attribute_nodes ⇒ Object
Get a list of attributes for the current node
101 102 103 104 105 |
# File 'lib/nokogiri/xml/reader.rb', line 101 def attribute_nodes nodes = attr_nodes nodes.each { |v| v.instance_variable_set(:@_r, self) } nodes end |
#attributes ⇒ Object
Get a list of attributes for the current node.
90 91 92 93 94 95 96 97 |
# File 'lib/nokogiri/xml/reader.rb', line 90 def attributes attrs_hash = attribute_nodes.each_with_object({}) do |node, hash| hash[node.name] = node.to_s end ns = namespaces attrs_hash.merge!(ns) if ns attrs_hash end |
#attributes? ⇒ Boolean
Does this node have attributes?
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'ext/nokogiri/xml_reader.c', line 98
static VALUE attributes_eh(VALUE self)
{
xmlTextReaderPtr reader;
int eh;
Data_Get_Struct(self, xmlTextReader, reader);
eh = has_attributes(reader);
if(eh == 0) return Qfalse;
if(eh == 1) return Qtrue;
return Qnil;
}
|
#base_uri ⇒ Object
base_uri
Get the xml:base of the node
386 387 388 389 390 391 392 393 394 395 396 |
# File 'ext/nokogiri/xml_reader.c', line 386
static VALUE base_uri(VALUE self)
{
xmlTextReaderPtr reader;
const char * base_uri;
Data_Get_Struct(self, xmlTextReader, reader);
base_uri = (const char *)xmlTextReaderBaseUri(reader);
if (base_uri == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(base_uri);
}
|
#default? ⇒ Boolean
Was an attribute generated from the default value in the DTD or schema?
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'ext/nokogiri/xml_reader.c', line 60
static VALUE default_eh(VALUE self)
{
xmlTextReaderPtr reader;
int eh;
Data_Get_Struct(self, xmlTextReader, reader);
eh = xmlTextReaderIsDefault(reader);
if(eh == 0) return Qfalse;
if(eh == 1) return Qtrue;
return Qnil;
}
|
#depth ⇒ Object
Get the depth of the node
242 243 244 245 246 247 248 249 250 251 252 |
# File 'ext/nokogiri/xml_reader.c', line 242
static VALUE depth(VALUE self)
{
xmlTextReaderPtr reader;
int depth;
Data_Get_Struct(self, xmlTextReader, reader);
depth = xmlTextReaderDepth(reader);
if(depth == -1) return Qnil;
return INT2NUM((long)depth);
}
|
#each ⇒ Object
Move the cursor through the document yielding the cursor to the block
109 110 111 112 113 |
# File 'lib/nokogiri/xml/reader.rb', line 109 def each while cursor = self.read yield cursor end end |
#empty_element?(#) ⇒ Boolean Also known as: self_closing?
Returns true if the current node is empty, otherwise false.
602 603 604 605 606 607 608 609 610 611 612 |
# File 'ext/nokogiri/xml_reader.c', line 602
static VALUE empty_element_p(VALUE self)
{
xmlTextReaderPtr reader;
Data_Get_Struct(self, xmlTextReader, reader);
if(xmlTextReaderIsEmptyElement(reader))
return Qtrue;
return Qfalse;
}
|
#inner_xml ⇒ Object
Read the contents of the current node, including child nodes and markup. Returns a utf-8 encoded string.
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'ext/nokogiri/xml_reader.c', line 464
static VALUE inner_xml(VALUE self)
{
xmlTextReaderPtr reader;
xmlChar* value;
VALUE str;
Data_Get_Struct(self, xmlTextReader, reader);
value = xmlTextReaderReadInnerXml(reader);
str = Qnil;
if(value) {
str = NOKOGIRI_STR_NEW2((char*)value);
xmlFree(value);
}
return str;
}
|
#lang ⇒ Object
Get the xml:lang scope within which the node resides.
278 279 280 281 282 283 284 285 286 287 288 |
# File 'ext/nokogiri/xml_reader.c', line 278
static VALUE lang(VALUE self)
{
xmlTextReaderPtr reader;
const char *lang;
Data_Get_Struct(self, xmlTextReader, reader);
lang = (const char *)xmlTextReaderConstXmlLang(reader);
if(lang == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(lang);
}
|
#local_name ⇒ Object
Get the local name of the node
350 351 352 353 354 355 356 357 358 359 360 |
# File 'ext/nokogiri/xml_reader.c', line 350
static VALUE local_name(VALUE self)
{
xmlTextReaderPtr reader;
const char *name;
Data_Get_Struct(self, xmlTextReader, reader);
name = (const char *)xmlTextReaderConstLocalName(reader);
if(name == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(name);
}
|
#name ⇒ Object
Get the name of the node. Returns a utf-8 encoded string.
368 369 370 371 372 373 374 375 376 377 378 |
# File 'ext/nokogiri/xml_reader.c', line 368
static VALUE name(VALUE self)
{
xmlTextReaderPtr reader;
const char *name;
Data_Get_Struct(self, xmlTextReader, reader);
name = (const char *)xmlTextReaderConstName(reader);
if(name == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(name);
}
|
#namespace_uri ⇒ Object
Get the URI defining the namespace associated with the node
332 333 334 335 336 337 338 339 340 341 342 |
# File 'ext/nokogiri/xml_reader.c', line 332
static VALUE namespace_uri(VALUE self)
{
xmlTextReaderPtr reader;
const char *uri;
Data_Get_Struct(self, xmlTextReader, reader);
uri = (const char *)xmlTextReaderConstNamespaceUri(reader);
if(uri == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(uri);
}
|
#namespaces ⇒ Object
Get a hash of namespaces for this Node
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'ext/nokogiri/xml_reader.c', line 117
static VALUE namespaces(VALUE self)
{
xmlTextReaderPtr reader;
xmlNodePtr ptr;
VALUE attr ;
Data_Get_Struct(self, xmlTextReader, reader);
attr = rb_hash_new() ;
if (! has_attributes(reader))
return attr ;
ptr = xmlTextReaderExpand(reader);
if(ptr == NULL) return Qnil;
Nokogiri_xml_node_namespaces(ptr, attr);
return attr ;
}
|
#node_type ⇒ Object
Get the type of readers current node
417 418 419 420 421 422 |
# File 'ext/nokogiri/xml_reader.c', line 417
static VALUE node_type(VALUE self)
{
xmlTextReaderPtr reader;
Data_Get_Struct(self, xmlTextReader, reader);
return INT2NUM((long)xmlTextReaderNodeType(reader));
}
|
#outer_xml ⇒ Object
Read the current node and its contents, including child nodes and markup. Returns a utf-8 encoded string.
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'ext/nokogiri/xml_reader.c', line 490
static VALUE outer_xml(VALUE self)
{
xmlTextReaderPtr reader;
xmlChar *value;
VALUE str = Qnil;
Data_Get_Struct(self, xmlTextReader, reader);
value = xmlTextReaderReadOuterXml(reader);
if(value) {
str = NOKOGIRI_STR_NEW2((char*)value);
xmlFree(value);
}
return str;
}
|
#prefix ⇒ Object
Get the shorthand reference to the namespace associated with the node.
314 315 316 317 318 319 320 321 322 323 324 |
# File 'ext/nokogiri/xml_reader.c', line 314
static VALUE prefix(VALUE self)
{
xmlTextReaderPtr reader;
const char *prefix;
Data_Get_Struct(self, xmlTextReader, reader);
prefix = (const char *)xmlTextReaderConstPrefix(reader);
if(prefix == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(prefix);
}
|
#read ⇒ Object
Move the Reader forward through the XML document.
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'ext/nokogiri/xml_reader.c', line 430
static VALUE read_more(VALUE self)
{
xmlTextReaderPtr reader;
xmlErrorPtr error;
VALUE error_list;
int ret;
Data_Get_Struct(self, xmlTextReader, reader);
error_list = rb_funcall(self, rb_intern("errors"), 0);
xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
ret = xmlTextReaderRead(reader);
xmlSetStructuredErrorFunc(NULL, NULL);
if(ret == 1) return self;
if(ret == 0) return Qnil;
error = xmlGetLastError();
if(error)
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(error));
else
rb_raise(rb_eRuntimeError, "Error pulling: %d", ret);
return Qnil;
}
|
#state ⇒ Object
Get the state of the reader
404 405 406 407 408 409 |
# File 'ext/nokogiri/xml_reader.c', line 404
static VALUE state(VALUE self)
{
xmlTextReaderPtr reader;
Data_Get_Struct(self, xmlTextReader, reader);
return INT2NUM((long)xmlTextReaderReadState(reader));
}
|
#value ⇒ Object
Get the text value of the node if present. Returns a utf-8 encoded string.
296 297 298 299 300 301 302 303 304 305 306 |
# File 'ext/nokogiri/xml_reader.c', line 296
static VALUE value(VALUE self)
{
xmlTextReaderPtr reader;
const char *value;
Data_Get_Struct(self, xmlTextReader, reader);
value = (const char *)xmlTextReaderConstValue(reader);
if(value == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(value);
}
|
#value? ⇒ Boolean
Does this node have a text value?
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'ext/nokogiri/xml_reader.c', line 79
static VALUE value_eh(VALUE self)
{
xmlTextReaderPtr reader;
int eh;
Data_Get_Struct(self, xmlTextReader, reader);
eh = xmlTextReaderHasValue(reader);
if(eh == 0) return Qfalse;
if(eh == 1) return Qtrue;
return Qnil;
}
|
#xml_version ⇒ Object
Get the XML version of the document being read
260 261 262 263 264 265 266 267 268 269 270 |
# File 'ext/nokogiri/xml_reader.c', line 260
static VALUE xml_version(VALUE self)
{
xmlTextReaderPtr reader;
const char *version;
Data_Get_Struct(self, xmlTextReader, reader);
version = (const char *)xmlTextReaderConstXmlVersion(reader);
if(version == NULL) return Qnil;
return NOKOGIRI_STR_NEW2(version);
}
|