Class: Tilia::Xml::Element::XmlFragment
- Inherits:
-
Object
- Object
- Tilia::Xml::Element::XmlFragment
- Includes:
- Tilia::Xml::Element
- Defined in:
- lib/tilia/xml/element/xml_fragment.rb
Overview
The XmlFragment element allows you to extract a portion of your xml tree, and get a well-formed xml string.
This goes a bit beyond ‘innerXml` and friends, as we’ll also match all the correct namespaces.
Please note that the XML fragment:
-
Will not have an <?xml declaration.
-
Or a DTD
-
It will have all the relevant xmlns attributes.
-
It may not have a root element.
Instance Attribute Summary collapse
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Class Method Summary collapse
-
.xml_deserialize(reader) ⇒ Object
The deserialize method is called during xml parsing.
Instance Method Summary collapse
-
#==(other) ⇒ Object
TODO: document.
-
#initialize(xml) ⇒ XmlFragment
constructor
A new instance of XmlFragment.
-
#xml_serialize(writer) ⇒ void
The xmlSerialize method is called during xml writing.
Methods included from XmlDeserializable
Constructor Details
#initialize(xml) ⇒ XmlFragment
Returns a new instance of XmlFragment.
19 20 21 |
# File 'lib/tilia/xml/element/xml_fragment.rb', line 19 def initialize(xml) @xml = xml end |
Instance Attribute Details
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
23 24 25 |
# File 'lib/tilia/xml/element/xml_fragment.rb', line 23 def xml @xml end |
Class Method Details
.xml_deserialize(reader) ⇒ Object
The deserialize method is called during xml parsing.
This method is called statictlly, this is because in theory this method may be used as a type of constructor, or factory method.
Often you want to return an instance of the current class, but you are free to return other data as well.
You are responsible for advancing the reader to the next element. Not doing anything will result in a never-ending loop.
If you just want to skip parsing for this element altogether, you can just call $reader->next();
$reader->parseInnerTree() will parse the entire sub-tree, and advance to the next element.
71 72 73 74 75 |
# File 'lib/tilia/xml/element/xml_fragment.rb', line 71 def self.xml_deserialize(reader) result = new(reader.read_inner_xml) reader.next result end |
Instance Method Details
#==(other) ⇒ Object
TODO: document
78 79 80 81 82 83 84 |
# File 'lib/tilia/xml/element/xml_fragment.rb', line 78 def ==(other) if other.is_a? self.class other.xml == @xml else false end end |
#xml_serialize(writer) ⇒ void
This method returns an undefined value.
The xmlSerialize method is called during xml writing.
Use the $writer argument to write its own xml serialization.
An important note: do not create a parent element. Any element implementing XmlSerializble should only ever write what’s considered its ‘inner xml’.
The parent of the current element is responsible for writing a containing element.
This allows serializers to be re-used for different element names.
If you are opening new elements, you must also close them again.
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 |
# File 'lib/tilia/xml/element/xml_fragment.rb', line 26 def xml_serialize(writer) reader = Reader.new # Wrapping the xml in a container, so root-less values can still be # parsed. xml = <<XML <?xml version="1.0"?> <xml-fragment xmlns="http://sabre.io/ns">#{@xml}</xml-fragment> XML reader.xml(xml) while reader.read if reader.depth < 1 # Skipping the root node. next end case reader.node_type when ::LibXML::XML::Reader::TYPE_ELEMENT writer.start_element(reader.clark) empty = reader.empty_element? while reader.move_to_next_attribute != 0 case reader.namespace_uri when '', nil # RUBY namespace_uri = nil ... writer.write_attribute(reader.local_name, reader.value) when 'http://www.w3.org/2000/xmlns/' # Skip namespace declarations else writer.write_attribute(reader.clark, reader.value) end end writer.end_element if empty when ::LibXML::XML::Reader::TYPE_CDATA, ::LibXML::XML::Reader::TYPE_TEXT writer.write_string(reader.value) when ::LibXML::XML::Reader::TYPE_END_ELEMENT writer.end_element end end end |