
The XMLElement class provides an abstraction of the underlying XML parser engine. It provides XML-related methods for the Pool and PoolElement classes
Methods
public class
public instance
Public class methods
build_xml
(xml, root_element)
Builds a XML document
xml: | String the XML document of the object |
root_element: | String Base xml element |
- return
- XML object for the underlying XML engine
[show source]
# File OpenNebula/XMLUtils.rb, line 70 70: def self.build_xml(xml, root_element) 71: begin 72: if NOKOGIRI 73: doc = Nokogiri::XML(xml).xpath("/#{root_element}") 74: else 75: doc = REXML::Document.new(xml).root 76: end 77: rescue Exception => e 78: return OpenNebula::Error.new(e.message) 79: end 80: 81: return doc 82: end
new
(xml=nil)
xml: | _opaque xml object_ an xml object as returned by build_xml |
[show source]
# File OpenNebula/XMLUtils.rb, line 41 41: def initialize(xml=nil) 42: @xml = xml 43: end
Public instance methods
[]
(key)
Extract an element from the XML description of the PoolElement. key::String The name of the element
- return
- String the value of the element
Examples:
['VID'] # gets VM id ['HISTORY/HOSTNAME'] # get the hostname from the history
[show source]
# File OpenNebula/XMLUtils.rb, line 89 89: def [](key) 90: if NOKOGIRI 91: element=@xml.xpath(key.to_s) 92: 93: if element.size == 0 94: return nil 95: end 96: else 97: element=@xml.elements[key.to_s] 98: end 99: 100: if element 101: element.text 102: end 103: end
attr
(key,name)
Gets an attribute from an elemenT
key: | String xpath for the element |
name: | String name of the attribute |
[show source]
# File OpenNebula/XMLUtils.rb, line 125 125: def attr(key,name) 126: value = nil 127: 128: if NOKOGIRI 129: element=@xml.xpath(key.to_s.upcase) 130: if element.size == 0 131: return nil 132: end 133: 134: attribute = element.attr(name) 135: 136: value = attribute.text if attribute != nil 137: else 138: element=@xml.elements[key.to_s.upcase] 139: 140: value = element.attributes[name] if element != nil 141: end 142: 143: return value 144: end
each
(xpath_str,&block)
Iterates over every Element in the XPath and calls the block with a a XMLElement
block: | Block |
[show source]
# File OpenNebula/XMLUtils.rb, line 149 149: def each(xpath_str,&block) 150: if NOKOGIRI 151: @xml.xpath(xpath_str).each { |pelem| 152: block.call XMLElement.new(pelem) 153: } 154: else 155: @xml.elements.each(xpath_str) { |pelem| 156: block.call XMLElement.new(pelem) 157: } 158: end 159: end
has_elements?
(xpath_str)
[show source]
# File OpenNebula/XMLUtils.rb, line 173 173: def has_elements?(xpath_str) 174: if NOKOGIRI 175: element = @xml.xpath(xpath_str.to_s.upcase) 176: return element != nil && element.children.size > 0 177: else 178: element = @xml.elements[xpath_str.to_s] 179: return element != nil && element.has_elements? 180: end 181: end
initialize_xml
(xml, root_element)
Initialize a XML document for the element
xml: | String the XML document of the object |
root_element: | String Base xml element |
[show source]
# File OpenNebula/XMLUtils.rb, line 48 48: def initialize_xml(xml, root_element) 49: @xml = XMLElement.build_xml(xml, root_element) 50: 51: if OpenNebula.is_error?(@xml) 52: @xml = nil 53: else 54: if NOKOGIRI 55: if @xml.size == 0 56: @xml = nil 57: end 58: else 59: if @xml.name != root_element 60: @xml = nil 61: end 62: end 63: end 64: end
retrieve_elements
(filter)
[show source]
# File OpenNebula/XMLUtils.rb, line 105 105: def retrieve_elements(filter) 106: ids_array = Array.new 107: if NOKOGIRI 108: elements=@xml.xpath(filter.to_s) 109: 110: if elements.size == 0 111: return nil 112: end 113: 114: elements.each{ |e| ids_array << e.text } 115: else 116: @xml.each(filter.to_s) { |e| ids_array << e.text } 117: end 118: 119: return ids_array 120: end
template_like_str
(root_element, indent=true)
[show source]
# File OpenNebula/XMLUtils.rb, line 187 187: def template_like_str(root_element, indent=true) 188: if NOKOGIRI 189: xml_template=@xml.xpath(root_element).to_s 190: rexml=REXML::Document.new(xml_template).root 191: else 192: rexml=@xml.elements[root_element] 193: end 194: 195: if indent 196: ind_enter="\n" 197: ind_tab=' ' 198: else 199: ind_enter='' 200: ind_tab=' ' 201: end 202: 203: str=rexml.collect {|n| 204: if n.class==REXML::Element 205: str_line="" 206: if n.has_elements? 207: str_line << n.name << "=[" << ind_enter 208: 209: str_line << n.collect {|n2| 210: if n2 && n2.class==REXML::Element 211: str = ind_tab + n2.name + "=" 212: str += n2.text if n2.text 213: str 214: end 215: }.compact.join(","+ind_enter) 216: str_line<<" ]" 217: else 218: str_line<<n.name << "=" << n.text.to_s 219: end 220: str_line 221: end 222: }.compact.join("\n") 223: 224: str 225: end
template_str
(indent=true)
[show source]
# File OpenNebula/XMLUtils.rb, line 183 183: def template_str(indent=true) 184: template_like_str('TEMPLATE', indent) 185: end
text
()
[show source]
# File OpenNebula/XMLUtils.rb, line 165 165: def text 166: if NOKOGIRI 167: @xml.content 168: else 169: @xml.text 170: end 171: end
to_hash
(hash={}, element=nil)
[show source]
# File OpenNebula/XMLUtils.rb, line 244 244: def to_hash(hash={}, element=nil) 245: element ||= @xml.document.root 246: 247: if NOKOGIRI 248: array = element.children 249: if array.length==1 and (array.first.text? or array.first.cdata?) 250: r = array.first.text 251: else 252: r = {} 253: array.each { |c| 254: if c.element? 255: to_hash(r, c) 256: end 257: } 258: end 259: else 260: r = {} 261: if element.has_elements? 262: element.each_element { |c| to_hash(r, c) } 263: elsif element.has_text? 264: r = element.text 265: end 266: end 267: 268: key = element.name 269: if hash.has_key?(key) 270: if hash[key].instance_of?(Array) 271: hash[key] << r 272: else 273: hash[key] = [hash[key], r] 274: end 275: else 276: hash[key] = r 277: end 278: 279: hash 280: end
to_xml
(pretty=false)
[show source]
# File OpenNebula/XMLUtils.rb, line 227 227: def to_xml(pretty=false) 228: if NOKOGIRI && pretty 229: str = @xml.to_xml 230: elsif REXML_FORMATTERS && pretty 231: str = String.new 232: 233: formatter = REXML::Formatters::Pretty.new 234: formatter.compact = true 235: 236: formatter.write(@xml,str) 237: else 238: str = @xml.to_s 239: end 240: 241: return str 242: end