class TaskJuggler::XMLDocument

This class provides a rather simple XML document generator. It provides basic features to create a tree of XMLElements and to generate a XML String or file. It’s much less powerful than REXML but provides a more efficient API to create XMLDocuments with lots of attributes.

Public Class Methods

new() { |block| ... } click to toggle source

Create an empty XML document.

# File lib/taskjuggler/XMLDocument.rb, line 25
def initialize(&block)
  @elements = block ? yield(block) : []
end

Public Instance Methods

<<(arg) click to toggle source

Add a top-level XMLElement.

# File lib/taskjuggler/XMLDocument.rb, line 30
def <<(arg)
  if arg.is_a?(Array)
    @elements += arg.flatten
  elsif arg.nil?
    # do nothing
  elsif arg.is_a?(XMLElement)
    @elements << arg
  else
    raise ArgumentError, "Unsupported argument of type #{arg.class}: " +
                         "#{arg.inspect}"
  end
end
to_s() click to toggle source

Produce the XMLDocument as String.

# File lib/taskjuggler/XMLDocument.rb, line 44
def to_s
  str = ''
  @elements.each do |element|
    str << element.to_s(0)
  end

  str
end
write(filename) click to toggle source

Write the XMLDocument to the specified file.

# File lib/taskjuggler/XMLDocument.rb, line 54
def write(filename)
  f = filename == '.' ? $stdout : File.new(filename, 'w')
  @elements.each do |element|
    f.puts element.to_s(0)
  end
  f.close unless f == $stdout
end