Node
Nokogiri::XML::Document is the main entry point for dealing with XML documents. The Document is created by parsing an XML document. See Nokogiri.XML()
For searching a Document, see Nokogiri::XML::Node#css and Nokogiri::XML::Node#xpath
(Not documented)
# File lib/nokogiri/xml/document.rb, line 14 def initialize *args @decorators = nil end
Create a new document with version (defaults to “1.0“)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
VALUE version, rest, rb_doc ;
rb_scan_args(argc, argv, "0*", &rest);
version = rb_ary_entry(rest, 0);
if (version == Qnil) {
version = rb_str_new2("1.0");
}
xmlDocPtr doc = xmlNewDoc((xmlChar *)StringValuePtr(version));
rb_doc = Nokogiri_wrap_xml_document(klass, doc);
rb_funcall2(rb_doc, rb_intern("initialize"), argc, argv);
return rb_doc ;
}
Parse an XML file. thing may be a String, or any object that responds to read and close such as an IO, or StringIO. url is resource where this document is located. encoding is the encoding that should be used when processing the document. options is a number that sets options in the parser, such as Nokogiri::XML::ParseOptions::RECOVER. See the constants in Nokogiri::XML::ParseOptions.
# File lib/nokogiri/xml/document.rb, line 90 def parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML, &block options = Nokogiri::XML::ParseOptions.new(options) if Fixnum === options # Give the options to the user yield options if block_given? if string_or_io.respond_to?(:read) url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil return self.read_io(string_or_io, url, encoding, options.to_i) end # read_memory pukes on empty docs return self.new if string_or_io.nil? or string_or_io.empty? self.read_memory(string_or_io, url, encoding, options.to_i) end
Create a new document from an IO object
static VALUE read_io( VALUE klass,
VALUE io,
VALUE url,
VALUE encoding,
VALUE options )
{
const char * c_url = (url == Qnil) ? NULL : StringValuePtr(url);
const char * c_enc = (encoding == Qnil) ? NULL : StringValuePtr(encoding);
VALUE error_list = rb_ary_new();
xmlInitParser();
xmlResetLastError();
xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
xmlDocPtr doc = xmlReadIO(
(xmlInputReadCallback)io_read_callback,
(xmlInputCloseCallback)io_close_callback,
(void *)io,
c_url,
c_enc,
NUM2INT(options)
);
xmlSetStructuredErrorFunc(NULL, NULL);
if(doc == NULL) {
xmlFreeDoc(doc);
xmlErrorPtr error = xmlGetLastError();
if(error)
rb_funcall(rb_mKernel, rb_intern("raise"), 1,
Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error)
);
else
rb_raise(rb_eRuntimeError, "Could not parse document");
return Qnil;
}
VALUE document = Nokogiri_wrap_xml_document(klass, doc);
rb_funcall(document, rb_intern("errors="), 1, error_list);
return document;
}
Create a new document from a String
static VALUE read_memory( VALUE klass,
VALUE string,
VALUE url,
VALUE encoding,
VALUE options )
{
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_LEN(string);
VALUE error_list = rb_ary_new();
xmlInitParser();
xmlResetLastError();
xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
xmlDocPtr doc = xmlReadMemory(c_buffer, len, c_url, c_enc, NUM2INT(options));
xmlSetStructuredErrorFunc(NULL, NULL);
if(doc == NULL) {
xmlFreeDoc(doc);
xmlErrorPtr error = xmlGetLastError();
if(error)
rb_funcall(rb_mKernel, rb_intern("raise"), 1,
Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error)
);
else
rb_raise(rb_eRuntimeError, "Could not parse document");
return Qnil;
}
VALUE document = Nokogiri_wrap_xml_document(klass, doc);
rb_funcall(document, rb_intern("errors="), 1, error_list);
return document;
}
Apply any decorators to node
# File lib/nokogiri/xml/document.rb, line 55 def decorate node return unless @decorators @decorators.each { |klass,list| next unless node.is_a?(klass) list.each { |moodule| node.extend(moodule) } } end
Get the list of decorators given key
# File lib/nokogiri/xml/document.rb, line 29 def decorators key @decorators ||= Hash.new @decorators[key] ||= [] end
A reference to self
# File lib/nokogiri/xml/document.rb, line 24 def document self end
Copy this Document. An optional depth may be passed in, but it defaults to a deep copy. 0 is a shallow copy, 1 is a deep copy.
static VALUE duplicate_node(int argc, VALUE *argv, VALUE self)
{
VALUE level;
if(rb_scan_args(argc, argv, "01", &level) == 0)
level = INT2NUM(1);
xmlDocPtr doc, dup;
Data_Get_Struct(self, xmlDoc, doc);
dup = xmlCopyDoc(doc, NUM2INT(level));
if(dup == NULL) return Qnil;
dup->type = doc->type;
return Nokogiri_wrap_xml_document(RBASIC(self)->klass, dup);
}
Get the encoding for this Document
static VALUE encoding(VALUE self)
{
xmlDocPtr doc;
Data_Get_Struct(self, xmlDoc, doc);
if(!doc->encoding) return Qnil;
return NOKOGIRI_STR_NEW2(doc->encoding, doc->encoding);
}
Set the encoding string for this Document
static VALUE set_encoding(VALUE self, VALUE encoding)
{
xmlDocPtr doc;
Data_Get_Struct(self, xmlDoc, doc);
doc->encoding = xmlStrdup((xmlChar *)StringValuePtr(encoding));
return encoding;
}
Create a Nokogiri::XML::DocumentFragment from tags
# File lib/nokogiri/xml/document.rb, line 74 def fragment tags DocumentFragment.new(self, tags) end
The name of this document. Always returns “document“
# File lib/nokogiri/xml/document.rb, line 19 def name 'document' end
Get the hash of namespaces on the root Nokogiri::XML::Node
# File lib/nokogiri/xml/document.rb, line 68 def namespaces root ? root.collect_namespaces : {} end
Get the root node for this document.
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(Qnil, root) ;
}
Set the root element on this document
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;
}
Explore a document with shortcut methods.
# File lib/nokogiri/xml/document.rb, line 44 def slop! unless decorators(XML::Node).include? Nokogiri::Decorators::Slop decorators(XML::Node) << Nokogiri::Decorators::Slop decorate! end self end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.