| Class | LibXML::XML::Document |
| In: |
ext/libxml/libxml.c
lib/libxml/document.rb |
| Parent: | Object |
Reads or writes an XML document:
Reading:
require 'xml'
doc = XML::Document.new()
doc.root = XML::Node.new('root_node')
doc.root << XML::Node.new('elem1')
doc.save('output.xml', format)
Writing:
require 'libxml'
doc = XML::Document.file('output.xml')
root = doc.root
Create a new XML::Document by parsing the specified file.
/*
* call-seq:
* XML::Document.file(filename) -> document
*
* Create a new XML::Document by parsing the specified
* file.
*/
VALUE
ruby_xml_document_new_file(VALUE class, VALUE filename) {
Create a new XML::Document, optionally specifying the XML version.
/*
* call-seq:
* XML::Document.new(xml_version = 1.0) -> document
*
* Create a new XML::Document, optionally specifying the
* XML version.
*/
VALUE
ruby_xml_document_new(int argc, VALUE *argv, VALUE class) {
Obtain this document‘s compression mode identifier.
/*
* call-seq:
* document.compression -> num
*
* Obtain this document's compression mode identifier.
*/
VALUE
ruby_xml_document_compression_get(VALUE self) {
Set this document‘s compression mode.
/*
* call-seq:
* document.compression = num
*
* Set this document's compression mode.
*/
VALUE
ruby_xml_document_compression_set(VALUE self, VALUE num) {
Determine whether this document is compressed.
/*
* call-seq:
* document.compression? -> (true|false)
*
* Determine whether this document is compressed.
*/
VALUE
ruby_xml_document_compression_q(VALUE self) {
Debug-dump this document‘s header to the specified IO stream. If no stream is specified, stdout is used.
/*
* call-seq:
* document.debug_dump_head([stream]) -> true
*
* Debug-dump this document's header to the specified IO stream.
* If no stream is specified, stdout is used.
*/
VALUE
ruby_xml_document_debug_dump_head(int argc, VALUE *argv, VALUE self) {
Deprecated in favour of format_dump.
/*
* call-seq:
* document.debug_format_dump([stream]) -> true
*
* *Deprecated* in favour of format_dump.
*/
VALUE
ruby_xml_document_debug_format_dump(int argc, VALUE *argv, VALUE self) {
Dump this document‘s XML to the specified IO stream. If no stream is specified, stdout is used.
/*
* call-seq:
* document.dump([stream]) -> true
*
* Dump this document's XML to the specified IO stream.
* If no stream is specified, stdout is used.
*/
VALUE
ruby_xml_document_dump(int argc, VALUE *argv, VALUE self) {
Return the nodes matching the specified xpath expression, optionally using the specified namespace. For more information about working with namespaces, please refer to the XML::XPath documentation.
Parameters:
document.find('/foo', 'xlink:http://www.w3.org/1999/xlink')
IMPORTANT - The returned XML::Node::Set must be freed before its associated document. In a running Ruby program this will happen automatically via Ruby‘s mark and sweep garbage collector. However, if the program exits, Ruby does not guarantee the order in which objects are freed (see blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17700). As a result, the associated document may be freed before the node list, which will cause a segmentation fault. To avoid this, use the following (non-ruby like) coding style:
nodes = doc.find('/header')
nodes.each do |node|
... do stuff ...
end
nodes = nil
GC.start
# File lib/libxml/document.rb, line 37
37: def find(xpath, nslist = nil)
38: context = XPath::Context.new(self)
39: context.node = self.root
40: context.register_namespaces_from_node(self.root)
41: context.register_namespaces(nslist) if nslist
42:
43: context.find(xpath)
44: end
Return the first node matching the specified xpath expression. For more information, please refer to the documentation for XML::Document#find.
# File lib/libxml/document.rb, line 49
49: def find_first(xpath, nslist = nil)
50: find(xpath, nslist).first
51: end
Dump this document‘s formatted XML to the specified IO stream. If no stream is specified, stdout is used. If spacing is specified, it must be a boolean that determines whether spacing is used.
/*
* call-seq:
* document.format_dump([stream], [spacing]) -> true
*
* Dump this document's formatted XML to the specified IO stream.
* If no stream is specified, stdout is used. If spacing is
* specified, it must be a boolean that determines whether
* spacing is used.
*/
VALUE
ruby_xml_document_format_dump(int argc, VALUE *argv, VALUE self) {
Obtain the previous node.
/*
* call-seq:
* document.prev -> node
*
* Obtain the previous node.
*/
VALUE
ruby_xml_document_prev_get(VALUE self) {
Determine whether there is a previous node.
/*
* call-seq:
* document.prev? -> (true|false)
*
* Determine whether there is a previous node.
*/
VALUE
ruby_xml_document_prev_q(VALUE self) {
Create a XML::Reader from the document. This is a shortcut to XML::Reader.walker().
/*
* call-seq:
* document.reader -> reader
*
* Create a XML::Reader from the document. This is a shortcut to
* XML::Reader.walker().
*/
static VALUE
ruby_xml_document_reader(VALUE self)
{
return ruby_xml_reader_new_walker(cXMLReader, self);
}
Save this document to the file given by filename, optionally formatting the output.
Parameters:
filename: The filename or URL of the new document format: Specifies whether formatting spaces should be added. returns: The number of bytes written or -1 in case of error.
/*
* call-seq:
* document.save(filename, format = false) -> int
*
* Save this document to the file given by filename,
* optionally formatting the output.
* Parameters:
* filename: The filename or URL of the new document
* format: Specifies whether formatting spaces should be added.
* returns: The number of bytes written or -1 in case of error.
*/
VALUE
ruby_xml_document_save(int argc, VALUE *argv, VALUE self) {
Determine whether this is a standalone document.
/*
* call-seq:
* document.standalone? -> (true|false)
*
* Determine whether this is a standalone document.
*/
VALUE
ruby_xml_document_standalone_q(VALUE self) {
Coerce this document to a string representation of it‘s XML. The default is to pretty format, but this depends Parser#indent_tree_output==true or Parser#default_keep_blanks==false.
The encoding is not applied to the document, but is encoding target of the resulting string.
/*
* call-seq:
* document.to_s({format=true,encoding) -> "xml"
*
* Coerce this document to a string representation
* of it's XML. The default is to pretty format, but this
* depends Parser#indent_tree_output==true or
* Parser#default_keep_blanks==false.
*
* The encoding is not applied to the document, but is
* encoding target of the resulting string.
*/
VALUE
ruby_xml_document_to_s(int argc, VALUE *argv, VALUE self) {
Obtain this document‘s source URL, if any.
/*
* call-seq:
* document.url -> "url"
*
* Obtain this document's source URL, if any.
*/
VALUE
ruby_xml_document_url_get(VALUE self) {
Validate this document against the specified XML::DTD.
/*
* call-seq:
* document.validate(dtd) -> (true|false)
*
* Validate this document against the specified XML::DTD.
*/
VALUE
ruby_xml_document_validate_dtd(VALUE self, VALUE dtd) {
Validate this document against the specified XML::RelaxNG.
If a block is provided it is used as an error handler for validaten errors. The block is called with two argument, the message and a flag indication if the message is an error (true) or a warning (false).
/*
* call-seq:
* document.validate_schema(relaxng) -> (true|false)
*
* Validate this document against the specified XML::RelaxNG.
*
* If a block is provided it is used as an error handler for validaten errors.
* The block is called with two argument, the message and a flag indication
* if the message is an error (true) or a warning (false).
*/
VALUE
ruby_xml_document_validate_relaxng(VALUE self, VALUE relaxng) {
Validate this document against the specified XML::Schema.
If a block is provided it is used as an error handler for validaten errors. The block is called with two argument, the message and a flag indication if the message is an error (true) or a warning (false).
/*
* call-seq:
* document.validate_schema(schema) -> (true|false)
*
* Validate this document against the specified XML::Schema.
*
* If a block is provided it is used as an error handler for validaten errors.
* The block is called with two argument, the message and a flag indication
* if the message is an error (true) or a warning (false).
*/
VALUE
ruby_xml_document_validate_schema(VALUE self, VALUE schema) {