Jared Williams wrote:
http://us3.php.net/manual/en/function.xmlwriter-write-raw.php
Anyone know the status of this function, if it does what I
want, and what version I can start using it?
I originally requested it. http://pecl.php.net/bugs/bug.php?id=6267 . I think
it'll appear in 5.2,
http://oss.backendmedia.com/PhP52 . Jared
Let me double-check that this does what I think it does. Take this code
for example:
<?php
//--------------------------------------------------
// build <img src="http://us2.php.net/images/php.gif" width="120"
height="67"/>
$I = xmlwriter_open_memory();
xmlwriter_start_element($I, "img");
xmlwriter_write_attribute($I, "src", "http://us2.php.net/images/php.gif");
xmlwriter_write_attribute($I, "width", "120");
xmlwriter_write_attribute($I, "height", "67");
xmlwriter_end_element($I);
$IMG_XHTML = xmlwriter_output_memory($I);
// build <a href="http://www.php.net">{REPLACE_ME}</a>
$A = xmlwriter_open_memory();
xmlwriter_start_element($A, "a");
xmlwriter_write_attribute($A, "href", "http://www.php.net/");
xmlwriter_text($A, "{REPLACE_ME}");
xmlwriter_end_element($A);
$A_XHTML = xmlwriter_output_memory($A);
// merge XML parts together
$xml_str = str_replace("{REPLACE_ME}", $IMG_XHTML, $A_XHTML);
print htmlspecialchars($xml_str);
// <a href="http://www.php.net/"><img
src="http://us2.php.net/images/php.gif" width="120" height="67"/></a>
//--------------------------------------------------
?>
Instead of having to merge the two XML strings back together, I could
write something like this instead?:
<?php
//--------------------------------------------------
// build <img src="http://us2.php.net/images/php.gif" width="120"
height="67"/>
$I = xmlwriter_open_memory();
xmlwriter_start_element($I, "img");
xmlwriter_write_attribute($I, "src", "http://us2.php.net/images/php.gif");
xmlwriter_write_attribute($I, "width", "120");
xmlwriter_write_attribute($I, "height", "67");
xmlwriter_end_element($I);
$IMG_XHTML = xmlwriter_output_memory($I);
// build <a href="http://www.php.net">$IMG_XHTML</a>
$A = xmlwriter_open_memory();
xmlwriter_start_element($A, "a");
xmlwriter_write_attribute($A, "href", "http://www.php.net/");
xmlwriter_write_raw($A, $IMG_XHTML); // merge already created XML
xmlwriter_end_element($A);
$xml_str = xmlwriter_output_memory($A);
// merge already done
print htmlspecialchars($xml_str);
// <a href="http://www.php.net/"><img
src="http://us2.php.net/images/php.gif" width="120" height="67"/></a>
//--------------------------------------------------
?>
I know this example is a little silly since a merge would never have
been necessary, but in my current code, I often construct parts of XML
documents inside other classes which need to all be stitched back
together to yield the final XHTML output. Being able to write 'raw'
XHTML into an existing node is helpful.
Documentation on XMLWriter is very sketchy overall. Looking at the
extension source, I see that I can use this as an object 'new
XMLWriter', but the documentation doesn't say anything about that. When
I wrap this functionality into my own custom XWriter object, I can
create shorthand like this:
<?php
//--------------------------------------------------
$XML = new XWriter("a", "href", "http://www.php.net");
$XML->append("img", "src", "http://us2.php.net/images/php.gif", "width",
"120", "height", "67");
$xml_str = $XML->toXML();
//--------------------------------------------------
?>
I'll be writing an article on XWriter some time in the next month on my
blog, but I need to use XWriter to create the blog software, first ;-)
Having the ability to xmlwriter_write_raw() would be a big help in
making XWriter more efficient.
Dante
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php