Module: ROXML::XML

Defined in:
lib/roxml/xml/parsers/libxml.rb,
lib/roxml/xml.rb,
lib/roxml/xml/parsers/nokogiri.rb

Overview

:nodoc:all

Defined Under Namespace

Classes: Node

Constant Summary

Document =
Nokogiri::XML::Document

Class Method Summary collapse

Class Method Details

.add_cdata(parent, content) ⇒ Object



23
24
25
# File 'lib/roxml/xml/parsers/libxml.rb', line 23

def add_cdata(parent, content)
  add_child(parent, LibXML::XML::Node.new_cdata(content))
end

.add_child(parent, child) ⇒ Object



27
28
29
30
# File 'lib/roxml/xml/parsers/libxml.rb', line 27

def add_child(parent, child)
  parent << child
  child
end

.add_node(parent, name) ⇒ Object



19
20
21
# File 'lib/roxml/xml/parsers/libxml.rb', line 19

def add_node(parent, name)
  add_child(parent, new_node(name))
end

.default_namespace(doc) ⇒ Object



48
49
50
51
52
# File 'lib/roxml/xml/parsers/libxml.rb', line 48

def default_namespace(doc)
  doc = doc.doc if doc.respond_to?(:doc)
  default = doc.root.namespaces.default
  default.prefix || 'xmlns' if default
end

.new_node(name) ⇒ Object



15
16
17
# File 'lib/roxml/xml/parsers/libxml.rb', line 15

def new_node(name)
  LibXML::XML::Node.new(name)
end

.parse_file(path) ⇒ Object



36
37
38
# File 'lib/roxml/xml/parsers/libxml.rb', line 36

def parse_file(path)
  LibXML::XML::Parser.file(path).parse
end

.parse_io(stream) ⇒ Object



40
41
42
# File 'lib/roxml/xml/parsers/libxml.rb', line 40

def parse_io(stream)
  LibXML::XML::Parser.io(stream).parse
end

.parse_string(string) ⇒ Object



32
33
34
# File 'lib/roxml/xml/parsers/libxml.rb', line 32

def parse_string(str_data)
  LibXML::XML::Parser.string(str_data).parse
end

.save_doc(doc, path) ⇒ Object



44
45
46
# File 'lib/roxml/xml/parsers/libxml.rb', line 44

def save_doc(doc, path)
  doc.save(path)
end

.search(xml, xpath, roxml_namespaces = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/roxml/xml/parsers/libxml.rb', line 54

def search(xml, xpath, roxml_namespaces = {})
  if xml.namespaces.default
    roxml_namespaces = {:xmlns => namespaces.default.href}.merge(roxml_namespaces)
  end
  if roxml_namespaces.present?
    xml.find(xpath, roxml_namespaces.map {|prefix, href| [prefix, href].join(':') })
  else
    xml.find(xpath)
  end
end

.set_attribute(node, name, value) ⇒ Object



7
8
9
# File 'lib/roxml/xml/parsers/libxml.rb', line 7

def set_attribute(node, name, value)
  node.attributes[name] = value
end

.set_content(node, content) ⇒ Object



11
12
13
# File 'lib/roxml/xml/parsers/libxml.rb', line 11

def set_content(node, content)
  node.content = content.gsub('&', '&amp;')
end