Class: Nokogiri::XML::Reader
- Inherits:
-
Object
- Object
- Nokogiri::XML::Reader
- Includes:
- Enumerable
- Defined in:
- lib/nokogiri/xml/reader.rb,
ext/nokogiri/xml_reader.c
Class Method Summary collapse
-
.from_memory(string, url = nil, encoding = nil, options = 0) ⇒ Object
Create a new reader that parses
string. -
.bool ⇒ Object
Set the global XML default for load external subsets.
- .new(*args) ⇒ Object
-
.parse_stylesheet_doc(document) ⇒ Object
Parse a stylesheet from
document. -
.read_memory(string, url, encoding, options) ⇒ Object
Read the HTML document contained in
stringwith givenurl,encoding, andoptions. -
.bool ⇒ Object
Set the global XML default for substitute entities.
Instance Method Summary collapse
-
#[](i) ⇒ Object
Get the node at index
i. -
#apply_to(document, params) ⇒ Object
Apply an XSLT stylesheet to an XML::Document.
-
#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.
-
#attributes ⇒ Object
Get a Hash of attributes for this node.
-
#attributes? ⇒ Boolean
Does this node have attributes?.
-
#default? ⇒ Boolean
Was an attribute generated from the default value in the DTD or schema?.
-
#depth ⇒ Object
Get the depth of the node.
- #each(&block) ⇒ Object
-
#encoding ⇒ Object
Get the encoding for the document.
-
#lang ⇒ Object
Get the xml:lang scope within which the node resides.
-
#length ⇒ Object
Get the length of the node set.
-
#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.
-
#parse_memory(data) ⇒ Object
Parse the document stored in
data. -
#prefix ⇒ Object
Get the shorthand reference to the namespace associated with the node.
-
#push(node) ⇒ Object
Append
nodeto the NodeSet. -
#read ⇒ Object
Move the Reader forward through the XML document.
-
#root ⇒ Object
Get the root node for this document.
-
#root= ⇒ Object
Set the root element on this document.
-
#serialize(document) ⇒ Object
Serialize
documentto an xml string. -
#state ⇒ Object
Get the state of the reader.
-
#type ⇒ Object
The type for this document.
-
#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.
Class Method Details
.from_memory(string, url = nil, encoding = nil, options = 0) ⇒ Object
Create a new reader that parses string
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'ext/nokogiri/xml_reader.c', line 363 static VALUE from_memory(int argc, VALUE *argv, VALUE klass) { VALUE rb_buffer, rb_url, encoding, ; const char * c_url = NULL; const char * c_encoding = NULL; int = 0; rb_scan_args(argc, argv, "13", &rb_buffer, &rb_url, &encoding, &); rb_buffer = StringValue(rb_buffer) ; if (RTEST(rb_url)) c_url = StringValuePtr(rb_url); if (RTEST(encoding)) c_encoding = StringValuePtr(rb_url); if (RTEST()) = NUM2INT(); xmlTextReaderPtr reader = xmlReaderForMemory( StringValuePtr(rb_buffer), NUM2INT(rb_funcall(rb_buffer, rb_intern("length"), 0)), c_url, c_encoding, ); if(reader == NULL) { xmlFreeTextReader(reader); rb_raise(rb_eRuntimeError, "couldn't create a parser"); } return Data_Wrap_Struct(klass, NULL, dealloc, reader); } |
.bool ⇒ Object
Set the global XML default for load external subsets.
129 130 131 132 133 |
# File 'ext/nokogiri/xml_document.c', line 129 static VALUE load_external_subsets_set(VALUE klass, VALUE value) { xmlLoadExtDtdDefaultValue = NUM2INT(value); return Qnil ; } |
.new(*args) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'ext/nokogiri/xml_document.c', line 101 static VALUE new(int argc, VALUE *argv, VALUE klass) { VALUE version; if(rb_scan_args(argc, argv, "01", &version) == 0) version = rb_str_new2("1.0"); xmlDocPtr doc = xmlNewDoc((xmlChar *)StringValuePtr(version)); return Nokogiri_wrap_xml_document(klass, doc); } |
.parse_stylesheet_doc(document) ⇒ Object
Parse a stylesheet from document.
20 21 22 23 24 25 26 27 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 20 static VALUE parse_stylesheet_doc(VALUE klass, VALUE xmldocobj) { xmlDocPtr xml ; xsltStylesheetPtr ss ; Data_Get_Struct(xmldocobj, xmlDoc, xml); ss = xsltParseStylesheetDoc(xmlCopyDoc(xml, 1)); /* 1 => recursive */ return Data_Wrap_Struct(klass, NULL, dealloc, ss); } |
.read_memory(string, url, encoding, options) ⇒ Object
Read the HTML document contained in string with given url, encoding, and options. See Nokogiri::HTML.parse
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'ext/nokogiri/xml_document.c', line 79 static VALUE read_memory( VALUE klass, VALUE string, VALUE url, VALUE encoding, VALUE ) { const char * c_buffer = StringValuePtr(string); const char * c_url = (url == Qnil) ? NULL : StringValuePtr(url); const char * c_enc = (encoding == Qnil) ? NULL : StringValuePtr(encoding); int len = NUM2INT(rb_funcall(string, rb_intern("length"), 0)); xmlInitParser(); xmlDocPtr doc = xmlReadMemory(c_buffer, len, c_url, c_enc, NUM2INT()); if(doc == NULL) { xmlFreeDoc(doc); rb_raise(rb_eRuntimeError, "Couldn't create a document"); } return Nokogiri_wrap_xml_document(klass, doc); } |
.bool ⇒ Object
Set the global XML default for substitute entities.
117 118 119 120 121 |
# File 'ext/nokogiri/xml_document.c', line 117 static VALUE substitute_entities_set(VALUE klass, VALUE value) { xmlSubstituteEntitiesDefault(NUM2INT(value)); return Qnil ; } |
Instance Method Details
#[](i) ⇒ Object
Get the node at index i
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'ext/nokogiri/xml_node_set.c', line 43 static VALUE index_at(VALUE self, VALUE number) { int i = NUM2INT(number); xmlNodeSetPtr node_set; Data_Get_Struct(self, xmlNodeSet, node_set); if(i >= node_set->nodeNr || abs(i) > node_set->nodeNr) return Qnil; if(i < 0) i = i + node_set->nodeNr; return Nokogiri_wrap_xml_node(node_set->nodeTab[i]); } |
#apply_to(document, params) ⇒ Object
Apply an XSLT stylesheet to an XML::Document. params is an array of strings used as XSLT parameters.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 60 static VALUE apply_to(int argc, VALUE* argv, VALUE self) { VALUE xmldoc, paramobj ; xmlDocPtr xml ; xmlDocPtr result ; xsltStylesheetPtr ss ; const char** params ; int param_len, j ; VALUE resultobj ; rb_scan_args(argc, argv, "11", &xmldoc, ¶mobj); if (paramobj == Qnil) { paramobj = rb_ary_new2(0) ; } Data_Get_Struct(xmldoc, xmlDoc, xml); Data_Get_Struct(self, xsltStylesheet, ss); param_len = RARRAY_LEN(paramobj); params = calloc((size_t)param_len+1, sizeof(char*)); for (j = 0 ; j < param_len ; j++) { VALUE entry = rb_ary_entry(paramobj, j); const char * ptr = StringValuePtr(entry); params[j] = ptr; } params[param_len] = 0 ; result = xsltApplyStylesheet(ss, xml, params); free(params); resultobj = Nokogiri_wrap_xml_document(0, result) ; return rb_funcall(self, rb_intern("serialize"), 1, resultobj); } |
#attribute(name) ⇒ Object
Get the value of attribute named name
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'ext/nokogiri/xml_reader.c', line 137 static VALUE reader_attribute(VALUE self, VALUE name) { xmlTextReaderPtr reader; xmlChar *value ; Data_Get_Struct(self, xmlTextReader, reader); if(name == Qnil) return Qnil; name = StringValue(name) ; value = xmlTextReaderGetAttribute(reader, (xmlChar*)StringValuePtr(name)); if(value == NULL) { /* this section is an attempt to workaround older versions of libxml that don't handle namespaces properly in all attribute-and-friends functions */ xmlChar *prefix = NULL ; xmlChar *localname = xmlSplitQName2((xmlChar*)StringValuePtr(name), &prefix); if (localname != NULL) { value = xmlTextReaderLookupNamespace(reader, localname); free(localname) ; } else { value = xmlTextReaderLookupNamespace(reader, prefix); } } if(value == NULL) return Qnil; VALUE rb_value = rb_str_new2((const char *)value); xmlFree(value); return rb_value; } |
#attribute_at(index) ⇒ Object
Get the value of attribute at index
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'ext/nokogiri/xml_reader.c', line 112 static VALUE attribute_at(VALUE self, VALUE index) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); if(index == Qnil) return Qnil; index = rb_funcall(index, rb_intern("to_i"), 0); xmlChar * value = xmlTextReaderGetAttributeNo( reader, NUM2INT(index) ); if(value == NULL) return Qnil; VALUE rb_value = rb_str_new2((const char *)value); xmlFree(value); return rb_value; } |
#attribute_count ⇒ Object
Get the number of attributes for the current node
172 173 174 175 176 177 178 179 180 |
# File 'ext/nokogiri/xml_reader.c', line 172 static VALUE attribute_count(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); int count = xmlTextReaderAttributeCount(reader); if(count == -1) return Qnil; return INT2NUM(count); } |
#attributes ⇒ Object
Get a Hash of attributes for this node
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'ext/nokogiri/xml_reader.c', line 85 static VALUE attributes(VALUE self) { xmlTextReaderPtr reader; VALUE attr ; Data_Get_Struct(self, xmlTextReader, reader); attr = rb_hash_new() ; if (! has_attributes(reader)) return attr ; xmlNodePtr ptr = xmlTextReaderExpand(reader); if(ptr == NULL) return Qnil; Nokogiri_xml_node_namespaces(ptr, attr); Nokogiri_xml_node_properties(ptr, attr); return attr ; } |
#attributes? ⇒ Boolean
Does this node have attributes?
68 69 70 71 72 73 74 75 76 77 |
# File 'ext/nokogiri/xml_reader.c', line 68 static VALUE attributes_eh(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); int eh = has_attributes(reader); if(eh == 0) return Qfalse; if(eh == 1) return Qtrue; return Qnil; } |
#default? ⇒ Boolean
Was an attribute generated from the default value in the DTD or schema?
34 35 36 37 38 39 40 41 42 43 |
# File 'ext/nokogiri/xml_reader.c', line 34 static VALUE default_eh(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); int eh = xmlTextReaderIsDefault(reader); if(eh == 0) return Qfalse; if(eh == 1) return Qtrue; return Qnil; } |
#depth ⇒ Object
Get the depth of the node
188 189 190 191 192 193 194 195 196 |
# File 'ext/nokogiri/xml_reader.c', line 188 static VALUE depth(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); int depth = xmlTextReaderDepth(reader); if(depth == -1) return Qnil; return INT2NUM(depth); } |
#each(&block) ⇒ Object
6 7 8 9 10 |
# File 'lib/nokogiri/xml/reader.rb', line 6 def each(&block) while node = self.read block.call(node) end end |
#encoding ⇒ Object
Get the encoding for the document
204 205 206 207 208 209 210 211 212 |
# File 'ext/nokogiri/xml_reader.c', line 204 static VALUE encoding(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * encoding = (const char *)xmlTextReaderConstEncoding(reader); if(encoding == NULL) return Qnil; return rb_str_new2(encoding); } |
#lang ⇒ Object
Get the xml:lang scope within which the node resides.
236 237 238 239 240 241 242 243 244 |
# File 'ext/nokogiri/xml_reader.c', line 236 static VALUE lang(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * lang = (const char *)xmlTextReaderConstXmlLang(reader); if(lang == NULL) return Qnil; return rb_str_new2(lang); } |
#length ⇒ Object
Get the length of the node set
9 10 11 12 13 14 15 16 17 18 |
# File 'ext/nokogiri/xml_node_set.c', line 9 static VALUE length(VALUE self) { xmlNodeSetPtr node_set; Data_Get_Struct(self, xmlNodeSet, node_set); if(node_set) return INT2NUM(node_set->nodeNr); return INT2NUM(0); } |
#local_name ⇒ Object
Get the local name of the node
300 301 302 303 304 305 306 307 308 |
# File 'ext/nokogiri/xml_reader.c', line 300 static VALUE local_name(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * name = (const char *)xmlTextReaderConstLocalName(reader); if(name == NULL) return Qnil; return rb_str_new2(name); } |
#name ⇒ Object
Get the name of the node
316 317 318 319 320 321 322 323 324 |
# File 'ext/nokogiri/xml_reader.c', line 316 static VALUE name(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * name = (const char *)xmlTextReaderConstName(reader); if(name == NULL) return Qnil; return rb_str_new2(name); } |
#namespace_uri ⇒ Object
Get the URI defining the namespace associated with the node
284 285 286 287 288 289 290 291 292 |
# File 'ext/nokogiri/xml_reader.c', line 284 static VALUE namespace_uri(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * uri = (const char *)xmlTextReaderConstNamespaceUri(reader); if(uri == NULL) return Qnil; return rb_str_new2(uri); } |
#parse_memory(data) ⇒ Object
Parse the document stored in data
11 12 13 14 15 16 17 18 19 20 21 |
# File 'ext/nokogiri/xml_sax_parser.c', line 11 static VALUE parse_memory(VALUE self, VALUE data) { xmlSAXHandlerPtr handler; Data_Get_Struct(self, xmlSAXHandler, handler); xmlSAXUserParseMemory( handler, (void *)self, StringValuePtr(data), NUM2INT(rb_funcall(data, rb_intern("length"), 0)) ); return data; } |
#prefix ⇒ Object
Get the shorthand reference to the namespace associated with the node.
268 269 270 271 272 273 274 275 276 |
# File 'ext/nokogiri/xml_reader.c', line 268 static VALUE prefix(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * prefix = (const char *)xmlTextReaderConstPrefix(reader); if(prefix == NULL) return Qnil; return rb_str_new2(prefix); } |
#push(node) ⇒ Object
Append node to the NodeSet.
26 27 28 29 30 31 32 33 34 35 |
# File 'ext/nokogiri/xml_node_set.c', line 26 static VALUE push(VALUE self, VALUE rb_node) { xmlNodeSetPtr node_set; xmlNodePtr node; Data_Get_Struct(self, xmlNodeSet, node_set); Data_Get_Struct(rb_node, xmlNode, node); xmlXPathNodeSetAdd(node_set, node); return self; } |
#read ⇒ Object
Move the Reader forward through the XML document.
345 346 347 348 349 350 351 352 353 354 355 |
# File 'ext/nokogiri/xml_reader.c', line 345 static VALUE read_more(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); int ret = xmlTextReaderRead(reader); if(ret == 1) return self; if(ret == 0) return Qnil; rb_raise(rb_eRuntimeError, "Error pulling: %d", ret); } |
#root ⇒ Object
Get the root node for this document.
68 69 70 71 72 73 74 75 76 77 |
# File 'ext/nokogiri/xml_document.c', line 68 static VALUE root(VALUE self) { xmlDocPtr doc; Data_Get_Struct(self, xmlDoc, doc); xmlNodePtr root = xmlDocGetRootElement(doc); if(!root) return Qnil; return Nokogiri_wrap_xml_node(root) ; } |
#root= ⇒ Object
Set the root element on this document
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'ext/nokogiri/xml_document.c', line 49 static VALUE set_root(VALUE self, VALUE root) { xmlDocPtr doc; xmlNodePtr new_root; Data_Get_Struct(self, xmlDoc, doc); Data_Get_Struct(root, xmlNode, new_root); xmlDocSetRootElement(doc, new_root); Nokogiri_xml_node_owned_set(new_root); return root; } |
#serialize(document) ⇒ Object
Serialize document to an xml string.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'ext/nokogiri/xml_document.c', line 30 static VALUE serialize(VALUE self) { xmlDocPtr doc; xmlChar *buf; int size; Data_Get_Struct(self, xmlDoc, doc); xmlDocDumpMemory(doc, &buf, &size); VALUE rb_str = rb_str_new((char *)buf, (long)size); free(buf); return rb_str; } |
#state ⇒ Object
Get the state of the reader
332 333 334 335 336 337 |
# File 'ext/nokogiri/xml_reader.c', line 332 static VALUE state(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); return INT2NUM(xmlTextReaderReadState(reader)); } |
#type ⇒ Object
The type for this document
54 55 56 57 58 59 |
# File 'ext/nokogiri/html_document.c', line 54 static VALUE type(VALUE self) { htmlDocPtr doc; Data_Get_Struct(self, xmlDoc, doc); return INT2NUM((int)doc->type); } |
#value ⇒ Object
Get the text value of the node if present
252 253 254 255 256 257 258 259 260 |
# File 'ext/nokogiri/xml_reader.c', line 252 static VALUE value(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * value = (const char *)xmlTextReaderConstValue(reader); if(value == NULL) return Qnil; return rb_str_new2(value); } |
#value? ⇒ Boolean
Does this node have a text value?
51 52 53 54 55 56 57 58 59 60 |
# File 'ext/nokogiri/xml_reader.c', line 51 static VALUE value_eh(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); int 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
220 221 222 223 224 225 226 227 228 |
# File 'ext/nokogiri/xml_reader.c', line 220 static VALUE xml_version(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); const char * version = (const char *)xmlTextReaderConstXmlVersion(reader); if(version == NULL) return Qnil; return rb_str_new2(version); } |