Extended maintenance of Ruby versions 1.8.7 and 1.9.2 will end on July 31, 2014. Read more

In Files

  • rexml/formatters/default.rb

REXML::Formatters::Default

Public Class Methods

new( ie_hack=false ) click to toggle source

Prints out the XML document with no formatting – except if id_hack is set.

ie_hack

If set to true, then inserts whitespace before the close of an empty tag, so that IE’s bad XML parser doesn’t choke.

 
               # File rexml/formatters/default.rb, line 10
def initialize( ie_hack=false )
  @ie_hack = ie_hack
end
            

Public Instance Methods

write( node, output ) click to toggle source

Writes the node to some output.

node

The node to write

output

A class implementing <<. Pass in an Output object to change the output encoding.

 
               # File rexml/formatters/default.rb, line 21
def write( node, output )
  case node

  when Document 
    if node.xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
      output = Output.new( output, node.xml_decl.encoding )
    end
    write_document( node, output )

  when Element
    write_element( node, output )

  when Declaration, ElementDecl, NotationDecl, ExternalEntity, Entity,
       Attribute, AttlistDecl
    node.write( output,-1 )

  when Instruction
    write_instruction( node, output )

  when DocType, XMLDecl
    node.write( output )

  when Comment
    write_comment( node, output )

  when CData
    write_cdata( node, output )

  when Text
    write_text( node, output )

  else
    raise Exception.new("XML FORMATTING ERROR")

  end
end
            

Protected Instance Methods

write_cdata( node, output ) click to toggle source
 
               # File rexml/formatters/default.rb, line 94
def write_cdata( node, output )
  output << CData::START
  output << node.to_s
  output << CData::STOP
end
            
write_comment( node, output ) click to toggle source
 
               # File rexml/formatters/default.rb, line 88
def write_comment( node, output )
  output << Comment::START
  output << node.to_s
  output << Comment::STOP
end
            
write_document( node, output ) click to toggle source
 
               # File rexml/formatters/default.rb, line 59
def write_document( node, output )
  node.children.each { |child| write( child, output ) }
end
            
write_element( node, output ) click to toggle source
 
               # File rexml/formatters/default.rb, line 63
def write_element( node, output )
  output << "<#{node.expanded_name}"

  node.attributes.each_attribute do |attr|
    output << " "
    attr.write( output )
  end unless node.attributes.empty?

  if node.children.empty?
    output << " " if @ie_hack
    output << "/" 
  else
    output << ">"
    node.children.each { |child|
      write( child, output )
    }
    output << "</#{node.expanded_name}"
  end
  output << ">"
end
            
write_instruction( node, output ) click to toggle source
 
               # File rexml/formatters/default.rb, line 100
def write_instruction( node, output )
  output << Instruction::START.sub(/\/u, '')
  output << node.target
  output << ' '
  output << node.content
  output << Instruction::STOP.sub(/\/u, '')
end
            
write_text( node, output ) click to toggle source
 
               # File rexml/formatters/default.rb, line 84
def write_text( node, output )
  output << node.to_s()
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus