Class: CFPropertyList::ReXMLParser
- Inherits:
-
ParserInterface
- Object
- ParserInterface
- CFPropertyList::ReXMLParser
- Defined in:
- lib/cfpropertylist/rbREXMLParser.rb
Overview
XML parser
Instance Method Summary collapse
- #append_node(parent, child) ⇒ Object
-
#load(opts) ⇒ Object
- read a XML file opts
-
:file - The filename of the file to load * :data - The data to parse.
-
- #new_node(name) ⇒ Object
- #new_text(val) ⇒ Object
-
#to_str(opts = {}) ⇒ Object
- serialize CFPropertyList object to XML opts = {}
-
Specify options: :formatted - Use indention and line breaks.
Instance Method Details
#append_node(parent, child) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/cfpropertylist/rbREXMLParser.rb', line 64 def append_node(parent, child) if child.is_a?(String) then parent.add_text child else parent.elements << child end parent end |
#load(opts) ⇒ Object
read a XML file
- opts
-
:file - The filename of the file to load
-
:data - The data to parse
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cfpropertylist/rbREXMLParser.rb', line 12 def load(opts) doc = nil if(opts.has_key?(:file)) then File.open(opts[:file], "rb") { |fd| doc = REXML::Document.new(fd) } else doc = REXML::Document.new(opts[:data]) end if doc root = doc.root.elements[1] return import_xml(root) end rescue REXML::ParseException => e raise CFFormatError.new('invalid XML: ' + e.) end |
#new_node(name) ⇒ Object
56 57 58 |
# File 'lib/cfpropertylist/rbREXMLParser.rb', line 56 def new_node(name) REXML::Element.new(name) end |
#new_text(val) ⇒ Object
60 61 62 |
# File 'lib/cfpropertylist/rbREXMLParser.rb', line 60 def new_text(val) val end |
#to_str(opts = {}) ⇒ Object
serialize CFPropertyList object to XML
- opts = {}
-
Specify options: :formatted - Use indention and line breaks
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cfpropertylist/rbREXMLParser.rb', line 31 def to_str(opts={}) doc = REXML::Document.new @doc = doc doc.context[:attribute_quote] = :quote doc.add_element 'plist', {'version' => '1.0'} doc.root << opts[:root].to_xml(self) formatter = if opts[:formatted] then f = REXML::Formatters::Pretty.new(2) f.compact = true f.width = Float::INFINITY f else REXML::Formatters::Default.new end str = formatter.write(doc.root, "") str1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + str + "\n" str1.force_encoding('UTF-8') if str1.respond_to?(:force_encoding) return str1 end |