Class: LibXML::XML::Reader
Relationships & Source Files | |
Inherits: | Object |
Defined in: | ext/libxml/ruby_xml_reader.c, ext/libxml/ruby_xml_reader.c |
Overview
The Reader
class provides a simpler, alternative way of parsing an ::LibXML::XML
document in contrast to Parser
or SaxParser
. A 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 #read.
The Reader
API closely matches the DOM Core specification and supports namespaces, xml:base, entity handling and DTDs.
To summarize, Reader
provides a far simpler API to use versus SaxParser
and is more memory efficient than using Parser
to create a DOM tree.
Example:
reader = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
reader.read
assert_equal('foo', reader.name)
assert_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 .document), strings (see Parser.string) and io objects (see Parser.io).
For a more in depth tutorial, albeit in C, see xmlsoft.org/xmlreader.html.
Constant Summary
-
DEFAULTATTRS =
# File 'ext/libxml/ruby_xml_reader.c', line 1207INT2FIX(XML_PARSER_DEFAULTATTRS)
-
LOADDTD =
Constants
INT2FIX(XML_PARSER_LOADDTD)
-
MODE_CLOSED =
# File 'ext/libxml/ruby_xml_reader.c', line 1240INT2FIX(XML_TEXTREADER_MODE_CLOSED)
-
MODE_EOF =
# File 'ext/libxml/ruby_xml_reader.c', line 1239INT2FIX(XML_TEXTREADER_MODE_EOF)
-
MODE_ERROR =
# File 'ext/libxml/ruby_xml_reader.c', line 1238INT2FIX(XML_TEXTREADER_MODE_ERROR)
-
MODE_INITIAL =
Read states
INT2FIX(XML_TEXTREADER_MODE_INITIAL)
-
MODE_INTERACTIVE =
# File 'ext/libxml/ruby_xml_reader.c', line 1237INT2FIX(XML_TEXTREADER_MODE_INTERACTIVE)
-
MODE_READING =
# File 'ext/libxml/ruby_xml_reader.c', line 1241INT2FIX(XML_TEXTREADER_MODE_READING)
-
SEVERITY_ERROR =
# File 'ext/libxml/ruby_xml_reader.c', line 1214INT2FIX(XML_PARSER_SEVERITY_ERROR)
-
SEVERITY_VALIDITY_ERROR =
# File 'ext/libxml/ruby_xml_reader.c', line 1212INT2FIX(XML_PARSER_SEVERITY_VALIDITY_ERROR)
-
SEVERITY_VALIDITY_WARNING =
# File 'ext/libxml/ruby_xml_reader.c', line 1211INT2FIX(XML_PARSER_SEVERITY_VALIDITY_WARNING)
-
SEVERITY_WARNING =
# File 'ext/libxml/ruby_xml_reader.c', line 1213INT2FIX(XML_PARSER_SEVERITY_WARNING)
-
SUBST_ENTITIES =
# File 'ext/libxml/ruby_xml_reader.c', line 1209INT2FIX(XML_PARSER_SUBST_ENTITIES)
-
TYPE_ATTRIBUTE =
# File 'ext/libxml/ruby_xml_reader.c', line 1218INT2FIX(XML_READER_TYPE_ATTRIBUTE)
-
TYPE_CDATA =
# File 'ext/libxml/ruby_xml_reader.c', line 1220INT2FIX(XML_READER_TYPE_CDATA)
-
TYPE_COMMENT =
# File 'ext/libxml/ruby_xml_reader.c', line 1224INT2FIX(XML_READER_TYPE_COMMENT)
-
TYPE_DOCUMENT =
# File 'ext/libxml/ruby_xml_reader.c', line 1225INT2FIX(XML_READER_TYPE_DOCUMENT)
-
TYPE_DOCUMENT_FRAGMENT =
# File 'ext/libxml/ruby_xml_reader.c', line 1227INT2FIX(XML_READER_TYPE_DOCUMENT_FRAGMENT)
-
TYPE_DOCUMENT_TYPE =
# File 'ext/libxml/ruby_xml_reader.c', line 1226INT2FIX(XML_READER_TYPE_DOCUMENT_TYPE)
-
TYPE_ELEMENT =
# File 'ext/libxml/ruby_xml_reader.c', line 1217INT2FIX(XML_READER_TYPE_ELEMENT)
-
TYPE_END_ELEMENT =
# File 'ext/libxml/ruby_xml_reader.c', line 1231INT2FIX(XML_READER_TYPE_END_ELEMENT)
-
TYPE_END_ENTITY =
# File 'ext/libxml/ruby_xml_reader.c', line 1232INT2FIX(XML_READER_TYPE_END_ENTITY)
-
TYPE_ENTITY =
# File 'ext/libxml/ruby_xml_reader.c', line 1222INT2FIX(XML_READER_TYPE_ENTITY)
-
TYPE_ENTITY_REFERENCE =
# File 'ext/libxml/ruby_xml_reader.c', line 1221INT2FIX(XML_READER_TYPE_ENTITY_REFERENCE)
-
TYPE_NONE =
# File 'ext/libxml/ruby_xml_reader.c', line 1216INT2FIX(XML_READER_TYPE_NONE)
-
TYPE_NOTATION =
# File 'ext/libxml/ruby_xml_reader.c', line 1228INT2FIX(XML_READER_TYPE_NOTATION)
-
TYPE_PROCESSING_INSTRUCTION =
# File 'ext/libxml/ruby_xml_reader.c', line 1223INT2FIX(XML_READER_TYPE_PROCESSING_INSTRUCTION)
-
TYPE_SIGNIFICANT_WHITESPACE =
# File 'ext/libxml/ruby_xml_reader.c', line 1230INT2FIX(XML_READER_TYPE_SIGNIFICANT_WHITESPACE)
-
TYPE_TEXT =
# File 'ext/libxml/ruby_xml_reader.c', line 1219INT2FIX(XML_READER_TYPE_TEXT)
-
TYPE_WHITESPACE =
# File 'ext/libxml/ruby_xml_reader.c', line 1229INT2FIX(XML_READER_TYPE_WHITESPACE)
-
TYPE_XML_DECLARATION =
# File 'ext/libxml/ruby_xml_reader.c', line 1233INT2FIX(XML_READER_TYPE_XML_DECLARATION)
-
VALIDATE =
# File 'ext/libxml/ruby_xml_reader.c', line 1208INT2FIX(XML_PARSER_VALIDATE)
Class Method Summary
-
XML::Reader.document(doc) ⇒ Reader
Create an new reader for the specified document.
-
XML::Reader.file(path) ⇒ Reader
Creates a new reader by parsing the specified file or uri.
-
XML::Reader.io(io) ⇒ Reader
Creates a new reader by parsing the specified io object.
-
XML::Reader.string(io) ⇒ Reader
Creates a new reader by parsing the specified string.
Instance Attribute Summary
-
#default? ⇒ Boolean
readonly
Return whether an Attribute node was generated from the default value defined in the DTD or schema.
-
#empty_element? ⇒ Boolean
readonly
Check if the current node is empty.
-
#has_attributes? ⇒ Boolean
readonly
Get whether the node has attributes.
-
#has_value? ⇒ Boolean
readonly
Get whether the node can have a text value.
-
#namespace_declaration? ⇒ Boolean
readonly
Determine whether the current node is a namespace declaration rather than a regular attribute.
-
#valid? ⇒ Boolean
readonly
Retrieve the validity status from the parser context.
Instance Method Summary
-
#[](key) ⇒ value
Provide the value of the attribute with the specified index (if
key
is an integer) or with the specified name (ifkey
is a string) relative to the containing element, as a string. -
#attribute_count ⇒ count
Provide the number of attributes of the current node.
-
#base_uri ⇒ URI
Determine the base URI of the node.
-
#byte_consumed ⇒ value
This method provides the current index of the parser used by the reader, relative to the start of the current entity.
-
#close ⇒ code
This method releases any resources allocated by the current instance changes the state to Closed and close any underlying input.
-
#column_number ⇒ Numeric
Provide the column number of the current parsing point.
-
#depth ⇒ depth
Get the depth of the node in the tree.
-
#document ⇒ doc
Hacking interface that provides access to the current document being accessed by the reader.
-
#encoding ⇒ XML::Encoding::UTF_8
Returns the encoding of the document being read.
-
#expand ⇒ node
Returns the current node and its full subtree.
-
#get_attribute(localName) ⇒ value
Provide the value of the attribute with the specified name relative to the containing element.
-
#get_attribute_no(index) ⇒ value
Provide the value of the attribute with the specified index relative to the containing element.
- #get_attribute_ns(name, ns)
-
#line_number ⇒ Numeric
Provide the line number of the current parsing point.
-
#local_name ⇒ name
Return the local name of the node.
-
#lookup_namespace(prefix) ⇒ value
Resolve a namespace prefix in the scope of the current element.
-
#move_to_attribute(localName) ⇒ code
Move the position of the current instance to the attribute with the specified name relative to the containing element.
-
#move_to_attribute_no(index) ⇒ code
Move the position of the current instance to the attribute with the specified index relative to the containing element.
-
#move_to_attribute_ns(localName, namespaceURI) ⇒ code
Move the position of the current instance to the attribute with the specified name and namespace relative to the containing element.
-
#move_to_element ⇒ code
Move the position of the current instance to the node that contains the current attribute node.
-
#move_to_first_attribute ⇒ code
Move the position of the current instance to the first attribute associated with the current node.
-
#move_to_next_attribute ⇒ code
Move the position of the current instance to the next attribute associated with the current node.
-
#name ⇒ name
Return the qualified name of the node.
-
#namespace_uri ⇒ URI
Determine the namespace URI of the node.
-
#next ⇒ code
Skip to the node following the current one in document order while avoiding the subtree if any.
-
#next_sibling ⇒ code
Skip to the node following the current one in document order while avoiding the subtree if any.
-
#node ⇒ XML::Node
Returns the reader’s current node.
-
#node_type ⇒ type
Get the node type of the current node.
-
#normalization ⇒ value
The value indicating whether to normalize white space and attribute values.
-
#prefix ⇒ prefix
Get a shorthand reference to the namespace associated with the node.
-
#quote_char ⇒ String
Get the quotation mark character used to enclose the value of an attribute, as an integer value (and -1 in case of error).
-
#read ⇒ Boolean
Causes the reader to move to the next node in the stream, exposing its properties.
-
#read_attribute_value ⇒ code
Parse an attribute value into one or more Text and EntityReference nodes.
-
#read_inner_xml ⇒ data
Read the contents of the current node, including child nodes and markup.
-
#read_outer_xml ⇒ data
Read the contents of the current node, including child nodes and markup.
-
#read_state ⇒ state
Get the read state of the reader.
-
#read_string ⇒ String
Read the contents of an element or a text node as a string.
-
#relax_ng_validate(rng) ⇒ Boolean
Use RelaxNG to validate the document as it is processed.
-
#schema_validate(schema) ⇒ Boolean
Use W3C XSD schema to validate the document as it is processed.
-
#standalone ⇒ code
Determine the standalone status of the document being read.
-
#value ⇒ text
Provide the text value of the node if present.
-
#xml_lang ⇒ value
Get the xml:lang scope within which the node resides.
-
#xml_version ⇒ version
Determine the
::LibXML::XML
version of the document being read.
Class Method Details
XML::Reader.document(doc) ⇒ Reader
Create an new reader for the specified document.
# File 'ext/libxml/ruby_xml_reader.c', line 93
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(xmlGetLastError()); return rxml_reader_wrap(xreader); }
XML::Reader.file(path) ⇒ Reader
XML::Reader.file(path, :encoding) ⇒ XML::Encoding::UTF_8
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.
- Controls the execution of the parser, defaults to 0.
Valid values are the constants defined on
XML::Parser::Options. Mutliple can be combined
by using Bitwise OR (|).
# File 'ext/libxml/ruby_xml_reader.c', line 125
static VALUE rxml_reader_file(int argc, VALUE *argv, VALUE klass) { VALUE path; VALUE options; rb_scan_args(argc, argv, "11", &path, &options); Check_Type(path, T_STRING); const char* xencoding = NULL; int xoptions = 0; if (!NIL_P(options)) { Check_Type(options, T_HASH); VALUE encoding = rb_hash_aref(options, BASE_URI_SYMBOL); xencoding = NIL_P(encoding) ? NULL : xmlGetCharEncodingName(NUM2INT(encoding)); VALUE parserOptions = rb_hash_aref(options, OPTIONS_SYMBOL); xoptions = NIL_P(parserOptions) ? 0 : NUM2INT(parserOptions); } xmlTextReaderPtr xreader = xmlReaderForFile(StringValueCStr(path), xencoding, xoptions); // Unfortunately libxml2 does not set xmlLastError and just returns a null reader if (xreader == NULL) rb_syserr_fail(ENOENT, StringValueCStr(path)); return rxml_reader_wrap(xreader); }
XML::Reader.io(io) ⇒ Reader
XML::Reader.io(io, :encoding) ⇒ XML::Encoding::UTF_8
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.
- Controls the execution of the parser, defaults to 0.
Valid values are the constants defined on
XML::Parser::Options. Mutliple can be combined
by using Bitwise OR (|).
# File 'ext/libxml/ruby_xml_reader.c', line 174
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(xmlGetLastError()); 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) ⇒ Reader
XML::Reader.string(io, :encoding) ⇒ XML::Encoding::UTF_8
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.
- Controls the execution of the parser, defaults to 0.
Valid values are the constants defined on
XML::Parser::Options. Mutliple can be combined
by using Bitwise OR (|).
# File 'ext/libxml/ruby_xml_reader.c', line 237
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), (int)RSTRING_LEN(string), xbaseurl, xencoding, xoptions); if (xreader == NULL) rxml_raise(xmlGetLastError()); return rxml_reader_wrap(xreader); }
Instance Attribute Details
#default? ⇒ Boolean
(readonly)
Return whether an Attribute node was generated from the default value defined in the DTD or schema.
# File 'ext/libxml/ruby_xml_reader.c', line 1089
static VALUE rxml_reader_default(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return xmlTextReaderIsDefault(xreader) ? Qtrue : Qfalse; }
#empty_element? ⇒ Boolean
(readonly)
Check if the current node is empty.
# File 'ext/libxml/ruby_xml_reader.c', line 1114
static VALUE rxml_reader_empty_element(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return xmlTextReaderIsEmptyElement(xreader) ? Qtrue : Qfalse; }
#has_attributes? ⇒ Boolean
(readonly)
Get whether the node has attributes.
# File 'ext/libxml/ruby_xml_reader.c', line 845
static VALUE rxml_reader_has_attributes(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return xmlTextReaderHasAttributes(xreader) ? Qtrue : Qfalse; }
#has_value? ⇒ Boolean
(readonly)
Get whether the node can have a text value.
# File 'ext/libxml/ruby_xml_reader.c', line 857
static VALUE rxml_reader_has_value(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return xmlTextReaderHasValue(xreader) ? Qtrue : Qfalse; }
#namespace_declaration? ⇒ Boolean
(readonly)
Determine whether the current node is a namespace declaration rather than a regular attribute.
# File 'ext/libxml/ruby_xml_reader.c', line 1102
static VALUE rxml_reader_namespace_declaration(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return xmlTextReaderIsNamespaceDecl(xreader) ? Qtrue : Qfalse; }
#valid? ⇒ Boolean
(readonly)
Retrieve the validity status from the parser context.
# File 'ext/libxml/ruby_xml_reader.c', line 1126
static VALUE rxml_reader_valid(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return xmlTextReaderIsValid(xreader) ? Qtrue : Qfalse; }
Instance Method Details
#[](key) ⇒ value
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.
# File 'ext/libxml/ruby_xml_reader.c', line 871
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_count ⇒ count
Provide the number of attributes of the current node.
# File 'ext/libxml/ruby_xml_reader.c', line 676
static VALUE rxml_reader_attr_count(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderAttributeCount(xreader)); }
#base_uri ⇒ URI
Determine the base URI of the node.
# File 'ext/libxml/ruby_xml_reader.c', line 714
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_consumed ⇒ value
This method provides the current index of the parser used by the reader, relative to the start of the current entity.
# File 'ext/libxml/ruby_xml_reader.c', line 1046
static VALUE rxml_reader_byte_consumed(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return LONG2NUM(xmlTextReaderByteConsumed(xreader)); }
#close ⇒ code
This method releases any resources allocated by the current instance changes the state to Closed and close any underlying input.
# File 'ext/libxml/ruby_xml_reader.c', line 283
static VALUE rxml_reader_close(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderClose(xreader)); }
#column_number ⇒ Numeric
Provide the column number of the current parsing point.
# File 'ext/libxml/ruby_xml_reader.c', line 1061
static VALUE rxml_reader_column_number(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2NUM(xmlTextReaderGetParserColumnNumber(xreader)); }
#depth ⇒ depth
Get the depth of the node in the tree.
# File 'ext/libxml/ruby_xml_reader.c', line 774
static VALUE rxml_reader_depth(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderDepth(xreader)); }
#document ⇒ doc
Hacking interface that provides access to the current document being accessed by the reader. NOTE: as a result of this call, the reader will not destroy the associated ::LibXML::XML
document. Instead, it will be destroyed when the returned document goes out of scope.
Returns: document
# File 'ext/libxml/ruby_xml_reader.c', line 1019
static VALUE rxml_reader_doc(VALUE self) { VALUE result = Qnil; xmlTextReaderPtr xreader = rxml_text_reader_get(self); xmlDocPtr xdoc = xmlTextReaderCurrentDoc(xreader); if (!xdoc) rb_raise(rb_eRuntimeError, "The reader does not have a document. Did you forget to call read?"); result = rxml_document_wrap(xdoc); // And now hook in a mark function to keep the document alive as long as the reader is valid RDATA(self)->dmark = (RUBY_DATA_FUNC)rxml_reader_mark; return result; }
#encoding ⇒ XML::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.
# File 'ext/libxml/ruby_xml_reader.c', line 698
static VALUE rxml_reader_encoding(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); const xmlChar *xencoding = xmlTextReaderConstEncoding(xreader); if (xencoding) return INT2NUM(xmlParseCharEncoding((const char*)xencoding)); else return INT2NUM(XML_CHAR_ENCODING_NONE); }
#expand ⇒ node
Returns the current node and its full subtree. Note the returned node is valid ONLY until the next read call. If you would like to preserve the node, or search it via xpath, call reader.doc first.
# File 'ext/libxml/ruby_xml_reader.c', line 989
static VALUE rxml_reader_expand(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); xmlNodePtr xnode = xmlTextReaderExpand(xreader); if (!xnode) { return Qnil; } else { /* We cannot call rxml_node_wrap here because its sets up a mark function for the node. But according to the libxml docs (http://xmlsoft.org/html/libxml-xmlreader.html#xmlTextReaderExpand) this is only valid until the next xmlTextReaderRead call. At that point the node is freed (from reading the libxml2 source code. So don't set a mark or free function, because they will get called in the next garbage collection run and cause a segfault.*/ return Data_Wrap_Struct(cXMLNode, NULL, NULL, xnode); } }
#get_attribute(localName) ⇒ value
Provide the value of the attribute with the specified name relative to the containing element.
# File 'ext/libxml/ruby_xml_reader.c', line 902
static VALUE rxml_reader_get_attribute(VALUE self, VALUE name) { VALUE result = Qnil; xmlChar *xattr; xmlTextReaderPtr xReader = rxml_text_reader_get(self); const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader); xattr = xmlTextReaderGetAttribute(xReader, (const xmlChar *) StringValueCStr(name)); if (xattr) { result = rxml_new_cstr(xattr, xencoding); xmlFree(xattr); } return result; }
#get_attribute_no(index) ⇒ value
Provide the value of the attribute with the specified index relative to the containing element.
# File 'ext/libxml/ruby_xml_reader.c', line 925
static VALUE rxml_reader_get_attribute_no(VALUE self, VALUE index) { VALUE result = Qnil; xmlChar *xattr; xmlTextReaderPtr xReader = rxml_text_reader_get(self); const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader); xattr = xmlTextReaderGetAttributeNo(xReader, FIX2INT(index)); if (xattr) { result = rxml_new_cstr(xattr, xencoding); xmlFree(xattr); } return result; }
#get_attribute_ns(name, ns)
[ GitHub ]# File 'ext/libxml/ruby_xml_reader.c', line 941
static VALUE rxml_reader_get_attribute_ns(VALUE self, VALUE name, VALUE ns) { VALUE result = Qnil; xmlChar *xattr; xmlTextReaderPtr xReader = rxml_text_reader_get(self); const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader); xattr = xmlTextReaderGetAttributeNs(xReader, (const xmlChar *) StringValueCStr(name), (const xmlChar *) StringValueCStr(ns)); if (xattr) { result = rxml_new_cstr(xattr, xencoding); xmlFree(xattr); } return result; }
#line_number ⇒ Numeric
Provide the line number of the current parsing point.
# File 'ext/libxml/ruby_xml_reader.c', line 1074
static VALUE rxml_reader_line_number(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2NUM(xmlTextReaderGetParserLineNumber(xreader)); }
#local_name ⇒ name
Return the local name of the node.
# File 'ext/libxml/ruby_xml_reader.c', line 661
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) ⇒ value
Resolve a namespace prefix in the scope of the current element. To return the default namespace, specify nil as #prefix.
# File 'ext/libxml/ruby_xml_reader.c', line 966
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(xnamespace, xencoding); xmlFree((void *)xnamespace); } return result; }
#move_to_attribute(localName) ⇒ code
Move the position of the current instance to the attribute with the specified name relative to the containing element.
# File 'ext/libxml/ruby_xml_reader.c', line 314
static VALUE rxml_reader_move_to_attr(VALUE self, VALUE val) { int ret; xmlTextReaderPtr xreader; xreader = rxml_text_reader_get(self); ret = xmlTextReaderMoveToAttribute(xreader, (const xmlChar *) StringValueCStr(val)); return INT2FIX(ret); }
#move_to_attribute_no(index) ⇒ code
Move the position of the current instance to the attribute with the specified index relative to the containing element.
# File 'ext/libxml/ruby_xml_reader.c', line 296
static VALUE rxml_reader_move_to_attr_no(VALUE self, VALUE index) { int ret; xmlTextReaderPtr xreader; xreader = rxml_text_reader_get(self); ret = xmlTextReaderMoveToAttributeNo(xreader, FIX2INT(index)); return INT2FIX(ret); }
#move_to_attribute_ns(localName, namespaceURI) ⇒ code
Move the position of the current instance to the attribute with the specified name and namespace relative to the containing element.
# File 'ext/libxml/ruby_xml_reader.c', line 333
static VALUE rxml_reader_move_to_attr_ns(VALUE self, VALUE name, VALUE ns) { int ret; xmlTextReaderPtr xreader; xreader = rxml_text_reader_get(self); ret = xmlTextReaderMoveToAttributeNs(xreader, (const xmlChar *) StringValueCStr(name), (const xmlChar *) StringValueCStr(ns)); return INT2FIX(ret); }
#move_to_element ⇒ code
Move the position of the current instance to the node that contains the current attribute node.
# File 'ext/libxml/ruby_xml_reader.c', line 379
static VALUE rxml_reader_move_to_element(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderMoveToElement(xreader)); }
#move_to_first_attribute ⇒ code
Move the position of the current instance to the first attribute associated with the current node.
# File 'ext/libxml/ruby_xml_reader.c', line 353
static VALUE rxml_reader_move_to_first_attr(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderMoveToFirstAttribute(xreader)); }
#move_to_next_attribute ⇒ code
Move the position of the current instance to the next attribute associated with the current node.
# File 'ext/libxml/ruby_xml_reader.c', line 366
static VALUE rxml_reader_move_to_next_attr(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderMoveToNextAttribute(xreader)); }
#name ⇒ name
Return the qualified name of the node.
# File 'ext/libxml/ruby_xml_reader.c', line 646
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_uri ⇒ URI
Determine the namespace URI of the node.
# File 'ext/libxml/ruby_xml_reader.c', line 729
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)); }
#next ⇒ code
Skip to the node following the current one in document order while avoiding the subtree if any.
# File 'ext/libxml/ruby_xml_reader.c', line 392
static VALUE rxml_reader_next(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderNext(xreader)); }
#next_sibling ⇒ code
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.
# File 'ext/libxml/ruby_xml_reader.c', line 406
static VALUE rxml_reader_next_sibling(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderNextSibling(xreader)); }
#node ⇒ XML::Node
# File 'ext/libxml/ruby_xml_reader.c', line 421
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_type ⇒ type
Get the node type of the current node. Reference: dotgnu.org/pnetlib-doc/System/Xml/XmlNodeType.html
# File 'ext/libxml/ruby_xml_reader.c', line 435
static VALUE rxml_reader_node_type(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderNodeType(xreader)); }
#normalization ⇒ value
The value indicating whether to normalize white space and attribute values. Since attribute value and end of line normalizations are a MUST in the ::LibXML::XML
specification only the value true is accepted. The broken bahaviour of accepting out of range character entities like � is of course not supported either.
Return 1 or -1 in case of error.
# File 'ext/libxml/ruby_xml_reader.c', line 453
static VALUE rxml_reader_normalization(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderNormalization(xreader)); }
#prefix ⇒ prefix
Get a shorthand reference to the namespace associated with the node.
# File 'ext/libxml/ruby_xml_reader.c', line 759
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_char ⇒ String
Get the quotation mark character used to enclose the value of an attribute, as an integer value (and -1 in case of error).
# File 'ext/libxml/ruby_xml_reader.c', line 787
static VALUE rxml_reader_quote_char(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderQuoteChar(xreader)); }
#read ⇒ Boolean
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.
# File 'ext/libxml/ruby_xml_reader.c', line 467
static VALUE rxml_reader_read(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); int result = xmlTextReaderRead(xreader); switch(result) { case -1: rxml_raise(xmlGetLastError()); 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_value ⇒ code
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.
# File 'ext/libxml/ruby_xml_reader.c', line 497
static VALUE rxml_reader_read_attr_value(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderReadAttributeValue(xreader)); }
#read_inner_xml ⇒ data
Read the contents of the current node, including child nodes and markup.
Return a string containing the ::LibXML::XML
content, or nil if the current node is neither an element nor attribute, or has no child nodes.
# File 'ext/libxml/ruby_xml_reader.c', line 512
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( xml, xencoding); xmlFree(xml); } return result; }
#read_outer_xml ⇒ data
Read the contents of the current node, including child nodes and markup.
Return a string containing the ::LibXML::XML
content, or nil if the current node is neither an element nor attribute, or has no child nodes.
# File 'ext/libxml/ruby_xml_reader.c', line 538
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( xml, xencoding); xmlFree(xml); } return result; }
#read_state ⇒ state
Get the read state of the reader.
# File 'ext/libxml/ruby_xml_reader.c', line 561
static VALUE rxml_reader_read_state(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderReadState(xreader)); }
#read_string ⇒ String
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.
# File 'ext/libxml/ruby_xml_reader.c', line 576
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( xml, xencoding); xmlFree(xml); } return result; }
#relax_ng_validate(rng) ⇒ Boolean
# File 'ext/libxml/ruby_xml_reader.c', line 604
static VALUE rxml_reader_relax_ng_validate(VALUE self, VALUE rng) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); xmlRelaxNGPtr xrelax; int status; Data_Get_Struct(rng, xmlRelaxNG, xrelax); status = xmlTextReaderRelaxNGSetSchema(xreader, xrelax); return (status == 0 ? Qtrue : Qfalse); }
#schema_validate(schema) ⇒ Boolean
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 ::LibXML::XML
Schema validation is deactivated.
Return false if if the schema’s validation could be (de)activated and true otherwise.
# File 'ext/libxml/ruby_xml_reader.c', line 627
static VALUE rxml_reader_schema_validate(VALUE self, VALUE xsd) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); xmlSchemaPtr xschema; int status; Data_Get_Struct(xsd, xmlSchema, xschema); status = xmlTextReaderSetSchema(xreader, xschema); return (status == 0 ? Qtrue : Qfalse); }
#standalone ⇒ code
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.
# File 'ext/libxml/ruby_xml_reader.c', line 803
static VALUE rxml_reader_standalone(VALUE self) { xmlTextReaderPtr xreader = rxml_text_reader_get(self); return INT2FIX(xmlTextReaderStandalone(xreader)); }
#value ⇒ text
Provide the text value of the node if present.
# File 'ext/libxml/ruby_xml_reader.c', line 744
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_lang ⇒ value
Get the xml:lang scope within which the node resides.
# File 'ext/libxml/ruby_xml_reader.c', line 815
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_version ⇒ version
Determine the ::LibXML::XML
version of the document being read.
# File 'ext/libxml/ruby_xml_reader.c', line 830
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)); }