Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating XML node with null value in php
    text
    copied!<p>I am creating a xml file using php. The resulting XML is.</p> <pre><code>&lt;xml&gt; &lt;data&gt; &lt;firstname&gt;Peter&lt;/firstname&gt; &lt;insertion&gt;V&lt;/insertion&gt; &lt;lastname&gt;John&lt;/lastname&gt; &lt;gender&gt;Male&lt;/gender&gt; &lt;/data&gt; &lt;/xml&gt; </code></pre> <p>But in case where a value is null the resulting xml is (<strong>look at node insertion</strong>).</p> <pre><code>&lt;xml&gt; &lt;data&gt; &lt;firstname&gt;Peter&lt;/firstname&gt; &lt;insertion/&gt; &lt;lastname&gt;John&lt;/lastname&gt; &lt;gender&gt;Male&lt;/gender&gt; &lt;/data&gt; &lt;/xml&gt; </code></pre> <p><strong>If a value is null I want the xml to be created such that it results in.</strong></p> <pre><code>&lt;xml&gt; &lt;data&gt; &lt;firstname&gt;Peter&lt;/firstname&gt; &lt;insertion&gt;&lt;/insertion&gt; &lt;lastname&gt;John&lt;/lastname&gt; &lt;gender&gt;Male&lt;/gender&gt; &lt;/data&gt; &lt;/xml&gt; </code></pre> <p>This is my code.</p> <pre><code> $doc = new DOMDocument('1.0'); $doc-&gt;formatOutput = true; $root = $doc-&gt;createElement('data'); $doc-&gt;appendChild($root); $data = $doc-&gt;createElement('data'); $fname = $doc-&gt;createElement('firstname'); $fname-&gt;appendChild( $doc-&gt;createTextNode($row['firstname'])); $data-&gt;appendChild($fname); $ins = $doc-&gt;createElement('insertion'); $ins-&gt;appendChild( $doc-&gt;createTextNode($row['insertion'])); $data-&gt;appendChild($ins); $lname = $doc-&gt;createElement('lastname'); $lname-&gt;appendChild( $doc-&gt;createTextNode($row['lastname'])); $data-&gt;appendChild($lname); $gender = $doc-&gt;createElement('gender'); $gender-&gt;appendChild( $doc-&gt;createTextNode($row['gender'])); $data-&gt;appendChild($gender); $root-&gt;appendChild($data); $doc-&gt;save($path . "test.xml"); </code></pre> <p>I am sending this xml as response after creating it. So in client side the <strong>lastname</strong> node is becoming a subnode of <strong>insertion</strong> when it is</p> <pre><code> &lt;insertion/&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload