LaVOZs

The World’s Largest Online Community for Developers

'; xml - how make node like this <node/> with php xmlwriter - LavOzs.Com

i need make a xml with php code, but i dont now how make a single node like <node/> using the xmlwriter method. example

<node>
     <element>ONE</element>
     <element>TWO</element>
     **<node_i_want_but_idontnow/>**
     <element>end</element>
</node>

Thanks in advance

<node-name/> is just an optimization. If an element node has no child nodes (elements, text, comments, ...). it can be closed directly. For the parser it is the same as <node-name></node-name>.

The serializer will apply that optimization by default. However if you add an empty string as content you will create a text child node and block the optimization:

$writer = new XMLWriter();
$writer->openURI('php://stdout');
$writer->setIndent(2);
$writer->startDocument();
$writer->startElement('node');

// element with content
$writer->writeElement('element', 'one');
// empty element
$writer->writeElement('another_element');
// element with empty child node
$writer->writeElement('another_element', '');

$writer->endElement();
$writer->endDocument();

Output:

<?xml version="1.0"?>
<node>
 <element>one</element>
 <another_element/>
 <another_element></another_element>
</node>
Related
How can I prevent SQL injection in PHP?
Deleting an element from an array in PHP
Secure hash and salt for PHP passwords
How do I make a redirect in PHP?
How do I comment out a block of tags in XML?
How do you parse and process HTML/XML in PHP?
Reference — What does this symbol mean in PHP?
Why shouldn't I use mysql_* functions in PHP?
How do I pass variables and data from PHP to JavaScript?