Class: LibXML::XML::Writer
- Inherits:
-
Object
- Object
- LibXML::XML::Writer
- Defined in:
- ext/libxml/ruby_xml_writer.c,
ext/libxml/ruby_xml_writer.c
Overview
The XML::Writer class provides a simpler, alternative way to build a valid XML document from scratch (forward-only) compared to a DOM approach (based on XML::Document class).
For a more in depth tutorial, albeit in C, see xmlsoft.org/xmlwriter.html
Class Method Summary collapse
-
.XML::Writer::document ⇒ XML::Writer
Creates a XML::Writer which will write into an in memory XML::Document.
-
.XML::Writer::file(path) ⇒ XML::Writer
Creates a XML::Writer object which will write XML into the file with the given name.
-
.XML::Writer::io(io) ⇒ XML::Writer
Creates a XML::Writer which will write XML directly into an IO object.
-
.XML::Writer::string ⇒ XML::Writer
Creates a XML::Writer which will write XML into memory, as string.
Instance Method Summary collapse
-
#end_attribute ⇒ Object
Ends an attribute, namespaced or not.
-
#end_cdata ⇒ Object
Ends current CDATA section.
-
#end_comment ⇒ Object
Ends current comment, returns
falseon failure. -
#end_document ⇒ Object
Ends current document.
-
#end_dtd ⇒ Object
Ends current DTD, returns
falseon failure. -
#end_dtd_attlist ⇒ Object
Ends current DTD attribute list, returns
falseon failure. -
#end_dtd_element ⇒ Object
Ends current DTD element, returns
falseon failure. -
#end_dtd_entity ⇒ Object
Ends current DTD entity, returns
falseon failure. -
#end_element ⇒ Object
Ends current element, namespaced or not.
-
#end_pi ⇒ Object
Ends current processing instruction.
-
#flush(empty? = true) ⇒ Object
Flushes the output buffer.
-
#write_full_end_element ⇒ Object
Ends current element, namespaced or not.
-
#result ⇒ (XML::Document|"string"|nil)
Returns the associated result object to the XML::Writer creation.
-
#set_indent(indentation) ⇒ Object
Toggles indentation on or off.
-
#set_indent_string(string) ⇒ Object
Sets the string to use to indent each element of the document.
-
#set_quote_char(...) ⇒ Object
Sets the character used to quote attributes.
-
#start_attribute(name) ⇒ Object
Starts an attribute.
-
#start_attribute_ns(prefix, name, namespaceURI) ⇒ Object
Starts a namespaced attribute.
-
#start_cdata ⇒ Object
Starts a new CDATA section.
-
#start_comment ⇒ Object
Starts a comment.
-
#start_document(*args) ⇒ Object
Starts a new document.
-
#start_dtd(qualifiedName, publicId, systemId) ⇒ Object
Starts a DTD.
-
#start_dtd_attlist(name) ⇒ Object
Starts a DTD attribute list (<!ATTLIST … >).
-
#start_dtd_element(qualifiedName) ⇒ Object
Starts a DTD element (<!ELEMENT … >).
-
#start_dtd_entity(name, pe = false) ⇒ Object
Starts a DTD entity (<!ENTITY … >).
-
#start_element(name) ⇒ Object
Starts a new element.
-
#start_element_ns(prefix, name, namespaceURI) ⇒ Object
Starts a new namespaced element.
-
#start_pi(target) ⇒ Object
Starts a new processing instruction.
-
#write_attribute(name, content) ⇒ Object
Writes a full attribute, all at once.
-
#write_attribute_ns(prefix, name, namespaceURI, content) ⇒ Object
Writes a full namespaced attribute, all at once.
-
#write_cdata(content) ⇒ Object
Writes a full CDATA section, all at once.
-
#write_comment(content) ⇒ Object
Writes a full comment tag, all at once.
-
#write_dtd(name[ [ [, publicId ], systemId ], subset ]) ⇒ Object
Writes a DTD, all at once.
-
#write_dtd_attlist(name, content) ⇒ Object
Writes a DTD attribute list, all at once.
-
#write_dtd_element(name, content) ⇒ Object
Writes a full DTD element, all at once.
-
#write_dtd_entity(name, publicId, systemId, ndataid, content, pe) ⇒ Object
Writes a DTD entity, all at once.
-
#write_dtd_external_entity(name, publicId, systemId, ndataid, pe) ⇒ Object
Writes a DTD external entity.
-
#write_dtd_external_entity_contents(publicId, systemId, ndataid) ⇒ Object
Writes the contents of a DTD external entity, all at once.
-
#write_dtd_internal_entity(name, content, pe) ⇒ Object
Writes a DTD internal entity, all at once.
-
#write_dtd_notation(name, publicId, systemId) ⇒ Object
Writes a DTD entity, all at once.
-
#write_element(name, content) ⇒ Object
Writes a full element tag, all at once.
-
#write_element_ns(prefix, name, namespaceURI, content) ⇒ Object
Writes a full namespaced element tag, all at once.
-
#write_pi(target, content) ⇒ Object
Writes a full CDATA tag, all at once.
-
#write_raw(content) ⇒ Object
Writes the string
contentas is, reserved characters are not translated to their associated entities. -
#write_string(content) ⇒ Object
Safely (problematic characters are internally translated to their associated named entities) writes a string into the current node (attribute, element, comment, …).
Class Method Details
.XML::Writer::document ⇒ XML::Writer
Creates a XML::Writer which will write into an in memory XML::Document
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'ext/libxml/ruby_xml_writer.c', line 186 static VALUE rxml_writer_doc(VALUE klass) { xmlDocPtr doc; rxml_writer_object* rwo; rwo = ALLOC(rxml_writer_object); rwo->buffer = NULL; rwo->output = Qnil; rwo->closed = 0; rwo->encoding = rb_utf8_encoding(); rwo->output_type = RXMLW_OUTPUT_DOC; if (NULL == (rwo->writer = xmlNewTextWriterDoc(&doc, 0))) { rxml_raise(xmlGetLastError()); } rwo->output = rxml_document_wrap(doc); return rxml_writer_wrap(rwo); } |
.XML::Writer::file(path) ⇒ XML::Writer
Creates a XML::Writer object which will write XML into the file with the given name.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'ext/libxml/ruby_xml_writer.c', line 136 static VALUE rxml_writer_file(VALUE klass, VALUE filename) { rxml_writer_object* rwo; rwo = ALLOC(rxml_writer_object); rwo->output = Qnil; rwo->buffer = NULL; rwo->closed = 0; rwo->encoding = rb_utf8_encoding(); rwo->output_type = RXMLW_OUTPUT_NONE; if (NULL == (rwo->writer = xmlNewTextWriterFilename(StringValueCStr(filename), 0))) { rxml_raise(xmlGetLastError()); } return rxml_writer_wrap(rwo); } |
.XML::Writer::io(io) ⇒ XML::Writer
Creates a XML::Writer which will write XML directly into an IO object.
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 |
# File 'ext/libxml/ruby_xml_writer.c', line 101 static VALUE rxml_writer_io(VALUE klass, VALUE io) { xmlOutputBufferPtr out; rxml_writer_object* rwo; rwo = ALLOC(rxml_writer_object); rwo->output = io; rwo->buffer = NULL; rwo->closed = 0; rwo->encoding = rb_enc_get(io); if (!rwo->encoding) rwo->encoding = rb_utf8_encoding(); rwo->output_type = RXMLW_OUTPUT_IO; xmlCharEncodingHandlerPtr encodingHdlr = xmlFindCharEncodingHandler(rwo->encoding->name); if (NULL == (out = xmlOutputBufferCreateIO(rxml_writer_write_callback, NULL, (void*)rwo, encodingHdlr))) { rxml_raise(xmlGetLastError()); } if (NULL == (rwo->writer = xmlNewTextWriter(out))) { rxml_raise(xmlGetLastError()); } return rxml_writer_wrap(rwo); } |
.XML::Writer::string ⇒ XML::Writer
Creates a XML::Writer which will write XML into memory, as string.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'ext/libxml/ruby_xml_writer.c', line 159 static VALUE rxml_writer_string(VALUE klass) { rxml_writer_object* rwo; rwo = ALLOC(rxml_writer_object); rwo->output = Qnil; rwo->closed = 0; rwo->encoding = rb_utf8_encoding(); rwo->output_type = RXMLW_OUTPUT_STRING; if (NULL == (rwo->buffer = xmlBufferCreate())) { rxml_raise(xmlGetLastError()); } if (NULL == (rwo->writer = xmlNewTextWriterMemory(rwo->buffer, 0))) { xmlBufferFree(rwo->buffer); rxml_raise(xmlGetLastError()); } return rxml_writer_wrap(rwo); } |
Instance Method Details
#end_attribute ⇒ Object
Ends an attribute, namespaced or not. Returns false on failure.
588 589 590 591 |
# File 'ext/libxml/ruby_xml_writer.c', line 588 static VALUE rxml_writer_end_attribute(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndAttribute); } |
#end_cdata ⇒ Object
Ends current CDATA section. Returns false on failure.
687 688 689 690 |
# File 'ext/libxml/ruby_xml_writer.c', line 687 static VALUE rxml_writer_end_cdata(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndCDATA); } |
#end_comment ⇒ Object
Ends current comment, returns false on failure. Note: libxml2 >= 2.6.7 required
611 612 613 614 |
# File 'ext/libxml/ruby_xml_writer.c', line 611 static VALUE rxml_writer_end_comment(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndComment); } |
#end_document ⇒ Object
Ends current document. Returns false on failure.
745 746 747 748 |
# File 'ext/libxml/ruby_xml_writer.c', line 745 static VALUE rxml_writer_end_document(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndDocument); } |
#end_dtd ⇒ Object
Ends current DTD, returns false on failure.
831 832 833 834 |
# File 'ext/libxml/ruby_xml_writer.c', line 831 static VALUE rxml_writer_end_dtd(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndDTD); } |
#end_dtd_attlist ⇒ Object
Ends current DTD attribute list, returns false on failure.
851 852 853 854 |
# File 'ext/libxml/ruby_xml_writer.c', line 851 static VALUE rxml_writer_end_dtd_attlist(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndDTDAttlist); } |
#end_dtd_element ⇒ Object
Ends current DTD element, returns false on failure.
861 862 863 864 |
# File 'ext/libxml/ruby_xml_writer.c', line 861 static VALUE rxml_writer_end_dtd_element(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndDTDElement); } |
#end_dtd_entity ⇒ Object
Ends current DTD entity, returns false on failure.
841 842 843 844 |
# File 'ext/libxml/ruby_xml_writer.c', line 841 static VALUE rxml_writer_end_dtd_entity(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndDTDEntity); } |
#end_element ⇒ Object
Ends current element, namespaced or not. Returns false on failure.
655 656 657 658 |
# File 'ext/libxml/ruby_xml_writer.c', line 655 static VALUE rxml_writer_end_element(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndElement); } |
#end_pi ⇒ Object
Ends current processing instruction. Returns false on failure.
765 766 767 768 |
# File 'ext/libxml/ruby_xml_writer.c', line 765 static VALUE rxml_writer_end_pi(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterEndPI); } |
#flush(empty? = true) ⇒ Object
Flushes the output buffer. Returns the number of written bytes or the current content of the internal buffer for a in memory XML::Writer. If empty? is true, and for a in memory XML::Writer, this internel buffer is empty.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'ext/libxml/ruby_xml_writer.c', line 216 static VALUE rxml_writer_flush(int argc, VALUE* argv, VALUE self) { int ret; VALUE empty; rxml_writer_object* rwo; rb_scan_args(argc, argv, "01", &empty); rwo = rxml_textwriter_get(self); if (-1 == (ret = xmlTextWriterFlush(rwo->writer))) { rxml_raise(xmlGetLastError()); } if (NULL != rwo->buffer) { VALUE content; content = rb_external_str_new_with_enc((const char*)rwo->buffer->content, rwo->buffer->use, rwo->encoding); if (NIL_P(empty) || RTEST(empty)) { /* nil = default value = true */ xmlBufferEmpty(rwo->buffer); } return content; } else { return INT2NUM(ret); } } |
#write_full_end_element ⇒ Object
Ends current element, namespaced or not. Returns false on failure. This method writes an end tag even if the element is empty (<foo></foo>), end_element does not (<foo/>).
667 668 669 670 |
# File 'ext/libxml/ruby_xml_writer.c', line 667 static VALUE rxml_writer_full_end_element(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterFullEndElement); } |
#result ⇒ (XML::Document|"string"|nil)
Returns the associated result object to the XML::Writer creation. A String for a XML::Writer object created with XML::Writer::string, a XML::Document with XML::Writer::document, etc.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'ext/libxml/ruby_xml_writer.c', line 255 static VALUE rxml_writer_result(VALUE self) { VALUE ret = Qnil; rxml_writer_object* rwo = rxml_textwriter_get(self); int bytesWritten = xmlTextWriterFlush(rwo->writer); if (bytesWritten == -1) { rxml_raise(xmlGetLastError()); } switch (rwo->output_type) { case RXMLW_OUTPUT_DOC: ret = rwo->output; break; case RXMLW_OUTPUT_STRING: ret = rb_external_str_new_with_enc((const char*)rwo->buffer->content, rwo->buffer->use, rwo->encoding); break; case RXMLW_OUTPUT_IO: case RXMLW_OUTPUT_NONE: break; default: rb_bug("unexpected output"); break; } return ret; } |
#set_indent(indentation) ⇒ Object
Toggles indentation on or off. Returns false on failure.
Availability: libxml2 >= 2.6.5
334 335 336 337 338 339 340 341 342 343 |
# File 'ext/libxml/ruby_xml_writer.c', line 334 static VALUE rxml_writer_set_indent(VALUE self, VALUE indentation) { int ret; rxml_writer_object* rwo; rwo = rxml_textwriter_get(self); ret = xmlTextWriterSetIndent(rwo->writer, RTEST(indentation)); return (-1 == ret ? Qfalse : Qtrue); } |
#set_indent_string(string) ⇒ Object
Sets the string to use to indent each element of the document. Don’t forget to enable indentation with set_indent. Returns false on failure.
Availability: libxml2 >= 2.6.5
354 355 356 357 |
# File 'ext/libxml/ruby_xml_writer.c', line 354 static VALUE rxml_writer_set_indent_string(VALUE self, VALUE indentation) { return invoke_single_arg_function(self, xmlTextWriterSetIndentString, indentation); } |
#set_quote_char(...) ⇒ Object
Sets the character used to quote attributes. Returns false on failure.
Notes:
-
only “ (default) and ‘ characters are valid
-
availability: libxml2 >= 2.9.0
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'ext/libxml/ruby_xml_writer.c', line 1030 static VALUE rxml_writer_set_quote_char(VALUE self, VALUE quotechar) { int ret; const char* xquotechar; rxml_writer_object* rwo; rwo = rxml_textwriter_get(self); xquotechar = StringValueCStr(quotechar); ret = xmlTextWriterSetQuoteChar(rwo->writer, (xmlChar)xquotechar[0]); return (-1 == ret ? Qfalse : Qtrue); } |
#start_attribute(name) ⇒ Object
Starts an attribute. Returns false on failure.
555 556 557 558 |
# File 'ext/libxml/ruby_xml_writer.c', line 555 static VALUE rxml_writer_start_attribute(VALUE self, VALUE name) { return invoke_single_arg_function(self, xmlTextWriterStartAttribute, name); } |
#start_attribute_ns(prefix, name, namespaceURI) ⇒ Object
Starts a namespaced attribute. Returns false on failure.
Note: by default, the xmlns: definition is repeated on every element. If you want the prefix, but don’t want the xmlns: declaration repeated, set namespaceURI to nil or omit it. Don’t forget to declare the namespace prefix somewhere earlier.
570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'ext/libxml/ruby_xml_writer.c', line 570 static VALUE rxml_writer_start_attribute_ns(int argc, VALUE* argv, VALUE self) { VALUE prefix, name, namespaceURI; rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI); rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {prefix, name, namespaceURI}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterStartAttributeNS(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2]); return (result == -1 ? Qfalse : Qtrue); } |
#start_cdata ⇒ Object
Starts a new CDATA section. Returns false on failure.
677 678 679 680 |
# File 'ext/libxml/ruby_xml_writer.c', line 677 static VALUE rxml_writer_start_cdata(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterStartCDATA); } |
#start_comment ⇒ Object
Starts a comment. Returns false on failure. Note: libxml2 >= 2.6.7 required
600 601 602 603 |
# File 'ext/libxml/ruby_xml_writer.c', line 600 static VALUE rxml_writer_start_comment(VALUE self) { return invoke_void_arg_function(self, xmlTextWriterStartComment); } |
#start_document ⇒ Object #start_document(: encoding) ⇒ XML::Encoding::UTF_8
Starts a new document. Returns false on failure.
You may provide an optional hash table to control XML header that will be generated. Valid options are:
-
encoding: the output document encoding, defaults to nil (= UTF-8). Valid
values are the encoding constants defined on XML::Encoding
-
standalone: nil (default) or a boolean to indicate if the document is
standalone or not
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
# File 'ext/libxml/ruby_xml_writer.c', line 706 static VALUE rxml_writer_start_document(int argc, VALUE* argv, VALUE self) { int ret; VALUE = Qnil; rxml_writer_object* rwo; const xmlChar* xencoding = NULL; const char* xstandalone = NULL; rb_scan_args(argc, argv, "01", &); if (!NIL_P()) { VALUE encoding, standalone; encoding = standalone = Qnil; Check_Type(, T_HASH); encoding = rb_hash_aref(, sEncoding); xencoding = NIL_P(encoding) ? NULL : (const xmlChar*)xmlGetCharEncodingName(NUM2INT(encoding)); standalone = rb_hash_aref(, sStandalone); if (NIL_P(standalone)) { xstandalone = NULL; } else { xstandalone = RTEST(standalone) ? "yes" : "no"; } } rwo = rxml_textwriter_get(self); rwo->encoding = rxml_figure_encoding(xencoding); ret = xmlTextWriterStartDocument(rwo->writer, NULL, (const char*)xencoding, xstandalone); return (-1 == ret ? Qfalse : Qtrue); } |
#start_dtd(qualifiedName, publicId, systemId) ⇒ Object
Starts a DTD. Returns false on failure.
775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'ext/libxml/ruby_xml_writer.c', line 775 static VALUE rxml_writer_start_dtd(int argc, VALUE* argv, VALUE self) { VALUE name, pubid, sysid; rb_scan_args(argc, argv, "12", &name, &pubid, &sysid); rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, pubid, sysid}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterStartDTD(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2]); return (result == -1 ? Qfalse : Qtrue); } |
#start_dtd_attlist(name) ⇒ Object
Starts a DTD attribute list (<!ATTLIST … >). Returns false on failure.
821 822 823 824 |
# File 'ext/libxml/ruby_xml_writer.c', line 821 static VALUE rxml_writer_start_dtd_attlist(VALUE self, VALUE name) { return invoke_single_arg_function(self, xmlTextWriterStartDTDAttlist, name); } |
#start_dtd_element(qualifiedName) ⇒ Object
Starts a DTD element (<!ELEMENT … >). Returns false on failure.
793 794 795 796 |
# File 'ext/libxml/ruby_xml_writer.c', line 793 static VALUE rxml_writer_start_dtd_element(VALUE self, VALUE name) { return invoke_single_arg_function(self, xmlTextWriterStartDTDElement, name); } |
#start_dtd_entity(name, pe = false) ⇒ Object
Starts a DTD entity (<!ENTITY … >). Returns false on failure.
803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'ext/libxml/ruby_xml_writer.c', line 803 static VALUE rxml_writer_start_dtd_entity(int argc, VALUE* argv, VALUE self) { VALUE name, pe; rb_scan_args(argc, argv, "11", &name, &pe); rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name}; const xmlChar* xmlStrings[] = {NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterStartDTDEntity(rwo->writer, RB_TEST(pe), xmlStrings[0]); return (result == -1 ? Qfalse : Qtrue); } |
#start_element(name) ⇒ Object
Starts a new element. Returns false on failure.
622 623 624 625 |
# File 'ext/libxml/ruby_xml_writer.c', line 622 static VALUE rxml_writer_start_element(VALUE self, VALUE name) { return invoke_single_arg_function(self, xmlTextWriterStartElement, name); } |
#start_element_ns(prefix, name, namespaceURI) ⇒ Object
Starts a new namespaced element. Returns false on failure.
Note: by default, the xmlns: definition is repeated on every element. If you want the prefix, but don’t want the xmlns: declaration repeated, set namespaceURI to nil or omit it. Don’t forget to declare the namespace prefix somewhere earlier.
637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'ext/libxml/ruby_xml_writer.c', line 637 static VALUE rxml_writer_start_element_ns(int argc, VALUE* argv, VALUE self) { VALUE prefix, name, namespaceURI; rb_scan_args(argc, argv, "21", &prefix, &name, &namespaceURI); rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {prefix, name, namespaceURI}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterStartElementNS(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2]); return (result == -1 ? Qfalse : Qtrue); } |
#start_pi(target) ⇒ Object
Starts a new processing instruction. Returns false on failure.
755 756 757 758 |
# File 'ext/libxml/ruby_xml_writer.c', line 755 static VALUE rxml_writer_start_pi(VALUE self, VALUE target) { return invoke_single_arg_function(self, xmlTextWriterStartPI, target); } |
#write_attribute(name, content) ⇒ Object
Writes a full attribute, all at once. Returns false on failure. Same as start_attribute(name) + write_string(content) + end_attribute.
471 472 473 474 475 476 477 478 479 |
# File 'ext/libxml/ruby_xml_writer.c', line 471 static VALUE rxml_writer_write_attribute(VALUE self, VALUE name, VALUE content) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, content}; const xmlChar* xmlStrings[] = {NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteAttribute(rwo->writer, xmlStrings[0], xmlStrings[1]); return (result == -1 ? Qfalse : Qtrue); } |
#write_attribute_ns(prefix, name, namespaceURI, content) ⇒ Object
Writes a full namespaced attribute, all at once. Returns false on failure. Same as start_attribute_ns(prefix, name, namespaceURI) + write_string(content) + end_attribute.
Notes:
-
by default, the xmlns: definition is repeated on every element. If you want
the prefix, but don’t want the xmlns: declaration repeated, set namespaceURI to nil or omit it. Don’t forget to declare the namespace prefix somewhere earlier.
-
contentcan be omitted too for an empty attribute
495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'ext/libxml/ruby_xml_writer.c', line 495 static VALUE rxml_writer_write_attribute_ns(int argc, VALUE* argv, VALUE self) { VALUE prefix, name, namespaceURI, content; rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content); rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {prefix, name, namespaceURI, content}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteAttributeNS(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2], xmlStrings[3]); return (result == -1 ? Qfalse : Qtrue); } |
#write_cdata(content) ⇒ Object
Writes a full CDATA section, all at once. Returns false on failure. This is equivalent to start_cdata + write_string(content) + end_cdata.
381 382 383 384 |
# File 'ext/libxml/ruby_xml_writer.c', line 381 static VALUE rxml_writer_write_cdata(VALUE self, VALUE content) { return invoke_single_arg_function(self, xmlTextWriterWriteCDATA, content); } |
#write_comment(content) ⇒ Object
Writes a full comment tag, all at once. Returns false on failure. This is equivalent to start_comment + write_string(content) + end_comment.
370 371 372 373 |
# File 'ext/libxml/ruby_xml_writer.c', line 370 static VALUE rxml_writer_write_comment(VALUE self, VALUE content) { return invoke_single_arg_function(self, xmlTextWriterWriteComment, content); } |
#write_dtd(name[ [ [, publicId ], systemId ], subset ]) ⇒ Object
Writes a DTD, all at once. Returns false on failure.
-
name: dtd name
-
publicId: external subset public identifier, use nil for a SYSTEM doctype
-
systemId: external subset system identifier
-
subset: content
Examples:
writer.write_dtd 'html'
#=> <!DOCTYPE html>
writer.write_dtd 'docbook', nil, 'http://www.docbook.org/xml/5.0/dtd/docbook.dtd'
#=> <!DOCTYPE docbook SYSTEM "http://www.docbook.org/xml/5.0/dtd/docbook.dtd">
writer.write_dtd 'html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
#=> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
writer.write_dtd 'person', nil, nil, '<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>'
#=> <!DOCTYPE person [<!ELEMENT person (firstname,lastname)><!ELEMENT firstname (#PCDATA)><!ELEMENT lastname (#PCDATA)>]>
885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'ext/libxml/ruby_xml_writer.c', line 885 static VALUE rxml_writer_write_dtd(int argc, VALUE* argv, VALUE self) { VALUE name, pubid, sysid, subset; rb_scan_args(argc, argv, "13", &name, &pubid, &sysid, &subset); rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, pubid, sysid, subset}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTD(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2], xmlStrings[3]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_attlist(name, content) ⇒ Object
Writes a DTD attribute list, all at once. Returns false on failure.
writer.write_dtd_attlist 'id', 'ID #IMPLIED'
#=> <!ATTLIST id ID #IMPLIED>
905 906 907 908 909 910 911 912 913 |
# File 'ext/libxml/ruby_xml_writer.c', line 905 static VALUE rxml_writer_write_dtd_attlist(VALUE self, VALUE name, VALUE content) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, content}; const xmlChar* xmlStrings[] = {NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDAttlist(rwo->writer, xmlStrings[0], xmlStrings[1]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_element(name, content) ⇒ Object
Writes a full DTD element, all at once. Returns false on failure.
writer.write_dtd_element 'person', '(firstname,lastname)'
#=> <!ELEMENT person (firstname,lastname)>
922 923 924 925 926 927 928 929 930 |
# File 'ext/libxml/ruby_xml_writer.c', line 922 static VALUE rxml_writer_write_dtd_element(VALUE self, VALUE name, VALUE content) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, content}; const xmlChar* xmlStrings[] = {NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDElement(rwo->writer, xmlStrings[0], xmlStrings[1]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_entity(name, publicId, systemId, ndataid, content, pe) ⇒ Object
Writes a DTD entity, all at once. Returns false on failure.
937 938 939 940 941 942 943 944 945 |
# File 'ext/libxml/ruby_xml_writer.c', line 937 static VALUE rxml_writer_write_dtd_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE content, VALUE pe) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, pubid, sysid, ndataid, content}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDEntity(rwo->writer, RB_TEST(pe), xmlStrings[0], xmlStrings[1], xmlStrings[2], xmlStrings[3], xmlStrings[4]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_external_entity(name, publicId, systemId, ndataid, pe) ⇒ Object
Writes a DTD external entity. The entity must have been started with start_dtd_entity. Returns false on failure.
-
name: the name of the DTD entity
-
publicId: the public identifier, which is an alternative to the system identifier
-
systemId: the system identifier, which is the URI of the DTD
-
ndataid: the xml notation name
-
pe:
trueif this is a parameter entity (to be used only in the DTD
itself), false if not
959 960 961 962 963 964 965 966 967 |
# File 'ext/libxml/ruby_xml_writer.c', line 959 static VALUE rxml_writer_write_dtd_external_entity(VALUE self, VALUE name, VALUE pubid, VALUE sysid, VALUE ndataid, VALUE pe) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, pubid, sysid, ndataid}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDExternalEntity(rwo->writer, RB_TEST(pe), xmlStrings[0], xmlStrings[1], xmlStrings[2], xmlStrings[3]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_external_entity_contents(publicId, systemId, ndataid) ⇒ Object
Writes the contents of a DTD external entity, all at once. Returns false on failure.
974 975 976 977 978 979 980 981 982 |
# File 'ext/libxml/ruby_xml_writer.c', line 974 static VALUE rxml_writer_write_dtd_external_entity_contents(VALUE self, VALUE pubid, VALUE sysid, VALUE ndataid) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {pubid, sysid, ndataid,}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDExternalEntityContents(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_internal_entity(name, content, pe) ⇒ Object
Writes a DTD internal entity, all at once. Returns false on failure.
Examples:
writer.write_dtd_entity 'Shape', '(rect|circle|poly|default)', true
#=> <!ENTITY % Shape "(rect|circle|poly|default)">
writer.write_dtd_entity 'delta', 'δ', false
#=> <!ENTITY delta "δ">
995 996 997 998 999 1000 1001 1002 1003 |
# File 'ext/libxml/ruby_xml_writer.c', line 995 static VALUE rxml_writer_write_dtd_internal_entity(VALUE self, VALUE name, VALUE content, VALUE pe) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, content}; const xmlChar* xmlStrings[] = {NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDInternalEntity(rwo->writer, RB_TEST(pe), xmlStrings[0], xmlStrings[1]); return (result == -1 ? Qfalse : Qtrue); } |
#write_dtd_notation(name, publicId, systemId) ⇒ Object
Writes a DTD entity, all at once. Returns false on failure.
1010 1011 1012 1013 1014 1015 1016 1017 1018 |
# File 'ext/libxml/ruby_xml_writer.c', line 1010 static VALUE rxml_writer_write_dtd_notation(VALUE self, VALUE name, VALUE pubid, VALUE sysid) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, pubid, sysid}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteDTDNotation(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2]); return (result == -1 ? Qfalse : Qtrue); } |
#write_element(name, content) ⇒ Object
Writes a full element tag, all at once. Returns false on failure. This is equivalent to start_element(name) + write_string(content) + end_element.
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'ext/libxml/ruby_xml_writer.c', line 397 static VALUE rxml_writer_write_element(int argc, VALUE* argv, VALUE self) { VALUE name, content; rb_scan_args(argc, argv, "11", &name, &content); if (Qnil == content) { if (Qfalse == rxml_writer_start_element(self, name)) { return Qfalse; } return rxml_writer_end_element(self); } else { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {name, content}; const xmlChar* xmlStrings[] = {NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteElement(rwo->writer, xmlStrings[0], xmlStrings[1]); return (result == -1 ? Qfalse : Qtrue); } } |
#write_element_ns(prefix, name, namespaceURI, content) ⇒ Object
Writes a full namespaced element tag, all at once. Returns false on failure. This is a shortcut for start_element_ns(prefix, name, namespaceURI) + write_string(content) + end_element.
Notes:
-
by default, the xmlns: definition is repeated on every element. If you want
the prefix, but don’t want the xmlns: declaration repeated, set namespaceURI to nil or omit it. Don’t forget to declare the namespace prefix somewhere earlier.
-
contentcan be omitted for an empty tag
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'ext/libxml/ruby_xml_writer.c', line 439 static VALUE rxml_writer_write_element_ns(int argc, VALUE* argv, VALUE self) { VALUE prefix, name, namespaceURI, content; rb_scan_args(argc, argv, "22", &prefix, &name, &namespaceURI, &content); if (Qnil == content) { VALUE argv[3] = { prefix, name, namespaceURI }; if (Qfalse == rxml_writer_start_element_ns(ARRAY_SIZE(argv), argv, self)) { return Qfalse; } return rxml_writer_end_element(self); } else { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {prefix, name, namespaceURI, content}; const xmlChar* xmlStrings[] = {NULL, NULL, NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWriteElementNS(rwo->writer, xmlStrings[0], xmlStrings[1], xmlStrings[2], xmlStrings[3]); return (result == -1 ? Qfalse : Qtrue); } } |
#write_pi(target, content) ⇒ Object
Writes a full CDATA tag, all at once. Returns false on failure. This is a shortcut for start_pi(target) + write_string(content) + end_pi.
514 515 516 517 518 519 520 521 522 |
# File 'ext/libxml/ruby_xml_writer.c', line 514 static VALUE rxml_writer_write_pi(VALUE self, VALUE target, VALUE content) { rxml_writer_object* rwo = rxml_textwriter_get(self); VALUE rubyStrings[] = {target, content}; const xmlChar* xmlStrings[] = {NULL, NULL}; encodeStrings(rwo->encoding, sizeof(rubyStrings)/sizeof(VALUE), rubyStrings, xmlStrings); int result = xmlTextWriterWritePI(rwo->writer, xmlStrings[0], xmlStrings[1]); return (result == -1 ? Qfalse : Qtrue); } |
#write_raw(content) ⇒ Object
Writes the string content as is, reserved characters are not translated to their associated entities. Returns false on failure. Consider write_string to handle them.
545 546 547 548 |
# File 'ext/libxml/ruby_xml_writer.c', line 545 static VALUE rxml_writer_write_raw(VALUE self, VALUE content) { return invoke_single_arg_function(self, xmlTextWriterWriteRaw, content); } |
#write_string(content) ⇒ Object
Safely (problematic characters are internally translated to their associated named entities) writes a string into the current node (attribute, element, comment, …). Returns false on failure.
533 534 535 536 |
# File 'ext/libxml/ruby_xml_writer.c', line 533 static VALUE rxml_writer_write_string(VALUE self, VALUE content) { return invoke_single_arg_function(self, xmlTextWriterWriteString, content); } |