File: nokogiri.rb

package info (click to toggle)
ruby-roxml 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 4,133; xml: 1,013; makefile: 7
file content (69 lines) | stat: -rw-r--r-- 1,592 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'nokogiri'

module ROXML
  module XML # :nodoc:all

    class << self
      def set_attribute(node, name, value)
        node[name] = value
      end

      def set_content(node, content)
        node.content = content
      end

      def new_node(name)
        Nokogiri::XML::Node.new(name, Document.new)
      end

      def add_node(parent, name)
        add_child(parent, Nokogiri::XML::Node.new(name, parent.document))
      end

      def add_cdata(parent, content)
        parent.add_child(Nokogiri::XML::CDATA.new(parent.document, content))
      end

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

      def parse_string(string)
        Nokogiri::XML(string)
      end

      def parse_file(path)
        path = path.sub('file:', '') if path.starts_with?('file:')
        parse_io(open(path))
      end

      def parse_io(stream)
        Nokogiri::XML(stream)
      end

      def save_doc(doc, path)
        open(path, 'w') do |file|
          file << doc.serialize
        end
      end

      def default_namespace(doc)
        doc = doc.document if doc.respond_to?(:document)
        'xmlns' if doc.root.namespaces['xmlns']
      end

      def search(xml, xpath, roxml_namespaces = {})
        case xml
        when Nokogiri::XML::Document
          xml.search(xpath, roxml_namespaces)
        else
          xpath = "./#{xpath}"
          (roxml_namespaces.present? ? xml.search(xpath, roxml_namespaces) : xml.search(xpath))
        end
      end
    end

    Document = Nokogiri::XML::Document
    Node = Nokogiri::XML::Node
  end
end