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(xmldocobj) ⇒ Object
commented out for now.
-
.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(Nokogiri: :XML::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 ⇒ Object
Serialize this document.
-
#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
361 362 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 |
# File 'ext/nokogiri/xml_reader.c', line 361 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( RSTRING(rb_buffer)->ptr, RSTRING(rb_buffer)->len, 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(xmldocobj) ⇒ Object
commented out for now.
12 13 14 15 16 17 18 19 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 12 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 = RSTRING(string)->len ; 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(Nokogiri: :XML::Document, params) ⇒ Object
Apply an XSLT stylesheet to an XML document. params is an array of strings.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 46 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++) { params[j] = RSTRING(rb_ary_entry(paramobj, 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
135 136 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 |
# File 'ext/nokogiri/xml_reader.c', line 135 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*)RSTRING(name)->ptr ); 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*)RSTRING(name)->ptr, &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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'ext/nokogiri/xml_reader.c', line 110 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
170 171 172 173 174 175 176 177 178 |
# File 'ext/nokogiri/xml_reader.c', line 170 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'ext/nokogiri/xml_reader.c', line 83 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?
66 67 68 69 70 71 72 73 74 75 |
# File 'ext/nokogiri/xml_reader.c', line 66 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?
32 33 34 35 36 37 38 39 40 41 |
# File 'ext/nokogiri/xml_reader.c', line 32 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
186 187 188 189 190 191 192 193 194 |
# File 'ext/nokogiri/xml_reader.c', line 186 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
202 203 204 205 206 207 208 209 210 |
# File 'ext/nokogiri/xml_reader.c', line 202 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.
234 235 236 237 238 239 240 241 242 |
# File 'ext/nokogiri/xml_reader.c', line 234 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
298 299 300 301 302 303 304 305 306 |
# File 'ext/nokogiri/xml_reader.c', line 298 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
314 315 316 317 318 319 320 321 322 |
# File 'ext/nokogiri/xml_reader.c', line 314 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
282 283 284 285 286 287 288 289 290 |
# File 'ext/nokogiri/xml_reader.c', line 282 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.
266 267 268 269 270 271 272 273 274 |
# File 'ext/nokogiri/xml_reader.c', line 266 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.
343 344 345 346 347 348 349 350 351 352 353 |
# File 'ext/nokogiri/xml_reader.c', line 343 static VALUE read(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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'ext/nokogiri/xml_document.c', line 50 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); return root; } |
#serialize ⇒ Object
Serialize this document
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'ext/nokogiri/xml_document.c', line 31 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
330 331 332 333 334 335 |
# File 'ext/nokogiri/xml_reader.c', line 330 static VALUE state(VALUE self) { xmlTextReaderPtr reader; Data_Get_Struct(self, xmlTextReader, reader); return INT2NUM(xmlTextReaderReadState(reader)); } |
#type ⇒ Object
The type for this document
59 60 61 62 63 64 |
# File 'ext/nokogiri/html_document.c', line 59 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
250 251 252 253 254 255 256 257 258 |
# File 'ext/nokogiri/xml_reader.c', line 250 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?
49 50 51 52 53 54 55 56 57 58 |
# File 'ext/nokogiri/xml_reader.c', line 49 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
218 219 220 221 222 223 224 225 226 |
# File 'ext/nokogiri/xml_reader.c', line 218 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); } |