Module: FHIR::InstanceXml
Instance Method Summary collapse
-
#hash_to_xml_node(name, hash, doc) ⇒ Object
Hash keys are ordered in Ruby 1.9 and later, so we can rely on their order to be the correct order for the XML serialization.
-
#to_xml ⇒ Object
This module includes methods to serialize or deserialize FHIR resources to and from XML.
Instance Method Details
#hash_to_xml_node(name, hash, doc) ⇒ Object
Hash keys are ordered in Ruby 1.9 and later, so we can rely on their order to be the correct order for the XML serialization.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fhir_models/bootstrap/xml.rb', line 90 def hash_to_xml_node(name, hash, doc) node = Nokogiri::XML::Node.new(name, doc) # if hash contains resourceType # create a child node with the name==resourceType # fill that, and place the child under the above `node` if hash['resourceType'].is_a?(String) && name != 'instance' child_name = hash['resourceType'] hash.delete('resourceType') child = hash_to_xml_node(child_name, hash, doc) node.add_child(child) return node end hash.each do |key, value| next if ['extension', 'modifierExtension'].include?(name) && key == 'url' next if key == 'id' && !versioned_fhir_module::RESOURCES.include?(name) case value when Hash node.add_child(hash_to_xml_node(key, value, doc)) when Array value.each do |v| if v.is_a?(Hash) node.add_child(hash_to_xml_node(key, v, doc)) else child = Nokogiri::XML::Node.new(key, doc) child.set_attribute('value', v) node.add_child(child) end end else child = Nokogiri::XML::Node.new(key, doc) if name == 'text' && key == 'div' child.set_attribute('xmlns', 'http://www.w3.org/1999/xhtml') html = value.strip if html.start_with?('<div') && html.end_with?('</div>') html = html[html.index('>') + 1..-7] end child.inner_html = html else child.set_attribute('value', value) end node.add_child(child) end end node.set_attribute('url', hash['url']) if ['extension', 'modifierExtension'].include?(name) node.set_attribute('id', hash['id']) if hash['id'] && !versioned_fhir_module::RESOURCES.include?(name) node end |
#to_xml ⇒ Object
This module includes methods to serialize or deserialize FHIR resources to and from XML.
78 79 80 81 82 83 84 85 86 |
# File 'lib/fhir_models/bootstrap/xml.rb', line 78 def to_xml hash = to_hash hash.delete('resourceType') doc = Nokogiri::XML::Document.new doc.encoding = 'utf-8' doc.root = hash_to_xml_node(resourceType, hash, doc) doc.root.default_namespace = 'http://hl7.org/fhir' doc.to_xml end |