class REXML::Formatters::Default

公共类方法

new( ie_hack=false ) 点击切换源码

打印 XML 文档,不进行任何格式化 - 除非设置了 ie_hack。

ie_hack

如果设置为 true,则在空标签的结尾前插入空格,以便 IE 的错误 XML 解析器不会崩溃。

# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 12
def initialize( ie_hack=false )
  @ie_hack = ie_hack
end

公共实例方法

write( node, output ) 点击切换源码

将节点写入某个输出。

node

要写入的节点

output

一个实现 << 的类。传入一个 Output 对象来更改输出编码。

# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 23
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

受保护的实例方法

write_cdata( node, output ) 点击切换源码
# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 98
def write_cdata( node, output )
  output << CData::START
  output << node.to_s
  output << CData::STOP
end
write_comment( node, output ) 点击切换源码
# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 92
def write_comment( node, output )
  output << Comment::START
  output << node.to_s
  output << Comment::STOP
end
write_document( node, output ) 点击切换源码
# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 61
def write_document( node, output )
  node.children.each { |child| write( child, output ) }
end
write_element( node, output ) 点击切换源码
# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 65
def write_element( node, output )
  output << "<#{node.expanded_name}"

  node.attributes.to_a.map { |a|
    Hash === a ? a.values : a
  }.flatten.sort_by {|attr| attr.name}.each 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 ) 点击切换源码
# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 104
def write_instruction( node, output )
  output << Instruction::START
  output << node.target
  content = node.content
  if content
    output << ' '
    output << content
  end
  output << Instruction::STOP
end
write_text( node, output ) 点击切换源码
# File rexml-3.4.0/lib/rexml/formatters/default.rb, line 88
def write_text( node, output )
  output << node.to_s()
end