I am creating an xml file using php. The resulting XML file.
<xml> <data> <firstname>Peter</firstname> <insertion>V</insertion> <lastname>John</lastname> <gender>Male</gender> </data> </xml>
But in the case where the value is null, the resulting xml is ( see node insertion ).
<xml> <data> <firstname>Peter</firstname> <insertion/> <lastname>John</lastname> <gender>Male</gender> </data> </xml>
If the value is null, I want the xml to be created so that it runs.
<xml> <data> <firstname>Peter</firstname> <insertion></insertion> <lastname>John</lastname> <gender>Male</gender> </data> </xml>
This is my code.
$doc = new DOMDocument('1.0'); $doc->formatOutput = true; $root = $doc->createElement('data'); $doc->appendChild($root); $data = $doc->createElement('data'); $fname = $doc->createElement('firstname'); $fname->appendChild( $doc->createTextNode($row['firstname'])); $data->appendChild($fname); $ins = $doc->createElement('insertion'); $ins->appendChild( $doc->createTextNode($row['insertion'])); $data->appendChild($ins); $lname = $doc->createElement('lastname'); $lname->appendChild( $doc->createTextNode($row['lastname'])); $data->appendChild($lname); $gender = $doc->createElement('gender'); $gender->appendChild( $doc->createTextNode($row['gender'])); $data->appendChild($gender); $root->appendChild($data); $doc->save($path . "test.xml");
I am sending this xml as an answer after creating it. So, on the client side, lastname node becomes an insertion subnom when it
<insertion/>
source share