D. Dante Lorenso wrote:
Rob Richards wrote:
Expected behavior. See comments within code snippet.
D. Dante Lorenso wrote:
I am using XMLWriter with PHP 5.1.4 and find that it doesn't behave
as I expect. I am under the impressing that until I call
'endElement', I should be free to continue adding attributes to an
opened element regardless of whether I have already added elements
or text below it. Look at this sample code:
<?php
//--------------------------------------------------
// create an object to write to an in-memory buffer
$XML = new XMLWriter();
$XML->openMemory();
// start new element and add 1 attribute
$XML->startElement("a");
$XML->writeAttribute("href", "http://www.google.com");
// add a text node
$XML->text("Google");
Here you just closed the starting element tag and moved into content.
So adding a text node closes the starting tag? That shouldn't happen.
Uhm, so where's the text supposed to go?
Seems to me that this is non-intuitive. I would expect that until I
call 'endElement' that the node is still constructible. Internally it
should work something like DOM where the node is not 'serialized' to
xml tag form until the endElement is called. That way, attributes
could still be defined even after 'text' nodes were appended.
You are missing the point of xmlWriter. It should happen. It provides a
forward only, non-cached means of writing to a stream. There are no
"nodes" or "trees" here. Calling endElement simply instructs the writer
to close any open content or attributes and finally the currently opened
element.
For your example code, just write all attributes prior to adding any
element content; otherwise you really want a tree based API like DOM or
SimpleXML where you can move backwards.
In any case, the behavior will not change. The behavior is defined in
the XMLTextWriter class in C#, from which XMLWriter is based on.
Rob
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php