moduleNokogirimoduleXML### 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#xpathclassDocument<Nokogiri::XML::Node### 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.defself.parsestring_or_io,url=nil,encoding=nil,options=ParseOptions::DEFAULT_XML,&blockoptions=Nokogiri::XML::ParseOptions.new(options)ifFixnum===options# Give the options to the useryieldoptionsifblock_given?ifstring_or_io.respond_to?(:read)url||=string_or_io.respond_to?(:path)?string_or_io.path:nilreturnread_io(string_or_io,url,encoding,options.to_i)end# read_memory pukes on empty docsreturnnewifstring_or_io.nil?orstring_or_io.empty?read_memory(string_or_io,url,encoding,options.to_i)end# A list of Nokogiri::XML::SyntaxError found when parsing a documentattr_accessor:errorsdefinitialize*args# :nodoc:@errors=[]@decorators=nilend### 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>#defcreate_elementname,*args,&blockelm=Nokogiri::XML::Element.new(name,self,&block)args.eachdo|arg|caseargwhenHasharg.each{|k,v|key=k.to_sifkey=~/^xmlns(:\w+)?$/ns_name=key.split(":",2)[1]elm.add_namespace_definitionns_name,vnextendelm[k.to_s]=v.to_s}elseelm.content=argendendelmend# Create a text node with +text+defcreate_text_nodetext,&blockNokogiri::XML::Text.new(text.to_s,self,&block)end# Create a CDATA element containing +text+defcreate_cdatatextNokogiri::XML::CDATA.new(self,text.to_s)end# The name of this document. Always returns "document"defname'document'end# A reference to +self+defdocumentselfend### 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.defcollect_namespacesns={}traverse{|j|ns.merge!(j.namespaces)}nsend# Get the list of decorators given +key+defdecoratorskey@decorators||=Hash.new@decorators[key]||=[]end### Validate this Document against it's DTD. Returns a list of errors on# the document or +nil+ when there is no DTD.defvalidatereturnnilunlessinternal_subsetinternal_subset.validateselfend### 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.#defslop!unlessdecorators(XML::Node).include?Nokogiri::Decorators::Slopdecorators(XML::Node)<<Nokogiri::Decorators::Slopdecorate!endselfend### Apply any decorators to +node+defdecoratenodereturnunless@decorators@decorators.each{|klass,list|nextunlessnode.is_a?(klass)list.each{|moodule|node.extend(moodule)}}endalias:to_xml:serializealias:clone:dup# Get the hash of namespaces on the root Nokogiri::XML::Nodedefnamespacesroot?root.namespaces:{}end### Create a Nokogiri::XML::DocumentFragment from +tags+# Returns an empty fragment if +tags+ is nil.deffragmenttags=nilDocumentFragment.new(self,tags,self.root)endundef_method:swap,:parent,:namespace,:default_namespace=undef_method:add_namespace_definition,:attributesundef_method:namespace_definitions,:line,:add_namespacedefadd_childchildraise"Document already has a root node"ifrootifchild.type==Node::DOCUMENT_FRAG_NODEraise"Document cannot have multiple root nodes"ifchild.children.size>1super(child.children.first)elsesuperendendalias:<<:add_childprivatedefimplied_xpath_context"/"enddefinspect_attributes[:name,:children]endendendend