Class: CFPropertyList::XML

Inherits:
ParserInterface show all
Defined in:
lib/rbXMLCFPropertyList.rb

Overview

XML parser

Instance Method Summary collapse

Instance Method Details

#load(opts) ⇒ Object

read a XML file

opts
  • :file - The filename of the file to load

  • :data - The data to parse



10
11
12
13
14
15
16
17
18
19
# File 'lib/rbXMLCFPropertyList.rb', line 10

def load(opts)
  if(opts.has_key?(:file)) then
    doc = LibXML::XML::Document.file(opts[:file],:options => LibXML::XML::Parser::Options::NOBLANKS|LibXML::XML::Parser::Options::NOENT)
  else
    doc = LibXML::XML::Document.string(opts[:data],:options => LibXML::XML::Parser::Options::NOBLANKS|LibXML::XML::Parser::Options::NOENT)
  end

  root = doc.root.first
  return import_xml(root)
end

#to_str(opts = {}) ⇒ Object

serialize CFPropertyList object to XML

opts = {}

Specify options: :formatted - Use indention and line breaks



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbXMLCFPropertyList.rb', line 23

def to_str(opts={})
  doc = LibXML::XML::Document.new

  doc.root = LibXML::XML::Node.new('plist')
  doc.encoding = LibXML::XML::Encoding::UTF_8

  doc.root['version'] = '1.0'
  doc.root << opts[:root].to_xml()

  # ugly hack, but there's no other possibility I know
  str = doc.to_s(:indent => opts[:formatted])
  str1 = String.new
  first = false
  str.each_line do
    |line|
    str1 << line
    unless(first) then
      str1 << "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" if line =~ /^\s*<\?xml/
    end

    first = true
  end

  return str1
end