class Nokogiri::XML::Node

Nokogiri::XML::Node is the primary API you’ll use to interact with your Document.

Attributes

A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes. For example:

node = Nokogiri::XML::DocumentFragment.parse("<a href='#foo' id='link'>link</a>").at_css("a")
node.to_html # => "<a href=\"#foo\" id=\"link\">link</a>"
node['href'] # => "#foo"
node.keys # => ["href", "id"]
node.values # => ["#foo", "link"]
node['class'] = 'green' # => "green"
node.to_html # => "<a href=\"#foo\" id=\"link\" class=\"green\">link</a>"

See the method group entitled Working With Node Attributes at Node for the full set of methods.

Navigation

Nokogiri::XML::Node also has methods that let you move around your tree:

#parent, #children, #next, #previous

Navigate up, down, or through siblings.

See the method group entitled Traversing Document Structure at Node for the full set of methods.

Serialization

When printing or otherwise emitting a document or a node (and its subtree), there are a few methods you might want to use:

#content, #text, #inner_text, #to_str

These methods will all **emit plaintext**, meaning that entities will be replaced (e.g., +&lt;+ will be replaced with +<+), meaning that any sanitizing will likely be un-done in the output.

#to_s, #to_xml, #to_html, #inner_html

These methods will all **emit properly-escaped markup**, meaning that it’s suitable for consumption by browsers, parsers, etc.

See the method group entitled Serialization and Generating Output at Node for the full set of methods.

Searching

You may search this node’s subtree using methods like #xpath and #css.

See the method group entitled Searching via XPath or CSS Queries at Node for the full set of methods.