Class | Nokogiri::XML::Document
In: | lib/nokogiri/xml/document.rb ext/nokogiri/xml_sax_push_parser.c
Parent: | Nokogiri::XML::Node

Nokogiri::XML::Document wraps an xml document.

### Methods

<< add_child collect_namespaces create_cdata create_element create_entity create_text_node decorate decorators document dup encoding encoding= fragment name namespaces new parse read_io read_memory remove_namespaces! root root= slop! to_java url validate version wrap

### External Aliases

serialize | -> | to_xml
dup | -> | clone

### Attributes

errors | [RW] | A list of Nokogiri::XML::SyntaxError found when parsing a document

### Public Class methods

new(version = default)

Create a new document with `version` (defaults to "1.0")

parse(string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML) {|options if block_given?| ...}

Parse an XML file. `string_or_io` 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.

read_io(io, url, encoding, options)

Create a new document from an IO object

read_memory(string, url, encoding, options)

Create a new document from a String

wrap(document)

`JRuby` Wraps Java‘s org.w3c.dom.document and returns Nokogiri::XML::Document

### Public Instance methods

<<(child)

Alias for add_child

add_child(child)
collect_namespaces()

Recursively get all namespaces from this node and its subtree and return them as a hash.

For example, given this document:

      <root xmlns:foo="bar">
        <bar xmlns:hello="world" />
      </root>

This method will return:

      { 'xmlns:foo' => 'bar', 'xmlns:hello' => 'world' }

WARNING: this method will clobber duplicate names in the keys. For example, given this document:

      <root xmlns:foo="bar">
        <bar xmlns:foo="baz" />
      </root>

The hash returned will look like this: { ‘xmlns:foo’ => ‘bar’ }

Non-prefixed default namespaces (as in "xmlns=") are not included in the hash.

Note this is a very expensive operation in current implementation, as it traverses the entire graph, and also has to bring each node accross the libxml bridge into a ruby object.

create_cdata(text)

Create a CDATA element containing `text`

create_element(name, *args, &block)

Create an element with `name`, and optionally setting the content and attributes.

      doc.create_element "div" # <div></div>
      doc.create_element "div", :class => "container" # <div class='container'></div>
      doc.create_element "div", "contents" # <div>contents</div>
      doc.create_element "div", "contents", :class => "container" # <div class='container'>contents</div>
      doc.create_element "div" { |node| node['class'] = "container" } # <div class='container'></div>

doc.create_entity(name, type, external_id, system_id, content)

Create a new entity named `name`.

`type` is an integer representing the type of entity to be created, and it defaults to Nokogiri::XML::EntityDecl::INTERNAL_GENERAL. See the constants on Nokogiri::XML::EntityDecl for more information.

`external_id`, `system_id`, and `content` set the External ID, System ID, and content respectively. All of these parameters are optional.

create_text_node(text, &block)

Create a text node with `text`

decorate(node)

Apply any decorators to `node`

decorators(key)

Get the list of decorators given `key`

document()

A reference to `self`

dup

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.

encoding

Get the encoding for this Document

encoding= encoding

Set the encoding string for this Document

fragment(tags = nil)

Create a Nokogiri::XML::DocumentFragment from `tags` Returns an empty fragment if `tags` is nil.

name()

The name of this document. Always returns "document"

namespaces()

Get the hash of namespaces on the root Nokogiri::XML::Node

remove_namespaces!

Remove all namespaces from all nodes in the document.

This could be useful for developers who either don‘t understand namespaces or don‘t care about them.

The following example shows a use case, and you can decide for yourself whether this is a good thing or not:

      doc = Nokogiri::XML <<-EOXML
         <root>
           <car xmlns:part="http://general-motors.com/">
             <part:tire>Michelin Model XGV</part:tire>
           </car>
           <bicycle xmlns:part="http://schwinn.com/">
             <part:tire>I'm a bicycle tire!</part:tire>
           </bicycle>
         </root>
         EOXML

      doc.xpath("//tire").to_s # => ""
      doc.xpath("//part:tire", "part" => "http://general-motors.com/").to_s # => "<part:tire>Michelin Model XGV</part:tire>"
      doc.xpath("//part:tire", "part" => "http://schwinn.com/").to_s # => "<part:tire>I'm a bicycle tire!</part:tire>"

      doc.remove_namespaces!

      doc.xpath("//tire").to_s # => "<tire>Michelin Model XGV</tire><tire>I'm a bicycle tire!</tire>"
      doc.xpath("//part:tire", "part" => "http://general-motors.com/").to_s # => ""
      doc.xpath("//part:tire", "part" => "http://schwinn.com/").to_s # => ""

For more information on why this probably is not a good thing in general, please direct your browser to tenderlovemaking.com/2009/04/23/namespaces-in-xml/

root

Get the root node for this document.

root=

Set the root element on this document

slop!()

Explore a document with shortcut methods. See Nokogiri::Slop for details.

Note that any nodes that have been instantiated before slop! is called will not be decorated with sloppy behavior. So, if you‘re in irb, the preferred idiom is:

      irb> doc = Nokogiri::Slop my_markup

and not

      irb> doc = Nokogiri::HTML my_markup
      ... followed by irb's implicit inspect (and therefore instantiation of every node) ...
      irb> doc.slop!
      ... which does absolutely nothing.

to_java()

`JRuby` Returns Java‘s org.w3c.dom.document of this Document.

url

Get the url name for this document.

validate()

Validate this Document against it‘s DTD. Returns a list of errors on the document or `nil` when there is no DTD.

version

Get the XML version for this Document

[Validate]