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
Source
# File lib/taskjuggler/XMLDocument.rb, line 26 def initialize(&block) @elements = block ? yield(block) : [] end
Create an empty XML document.
Public Instance Methods
Source
# File lib/taskjuggler/XMLDocument.rb, line 31 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
Add a top-level XMLElement
.
Source
# File lib/taskjuggler/XMLDocument.rb, line 45 def to_s str = +'' @elements.each do |element| str << element.to_s(0) end str end
Produce the XMLDocument
as String
.
Source
# File lib/taskjuggler/XMLDocument.rb, line 55 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
Write the XMLDocument
to the specified file.