Creating XML node with null value in php

pete20r2

New Member
I am creating a xml file using php.The resulting XML is.\[code\]<xml> <data> <firstname>Peter</firstname> <insertion>V</insertion> <lastname>John</lastname> <gender>Male</gender> </data></xml>\[/code\]But in case where a value is null the resulting xml is (look at node insertion).\[code\]<xml> <data> <firstname>Peter</firstname> <insertion/> <lastname>John</lastname> <gender>Male</gender> </data></xml>\[/code\]If a value is null I want the xml to be created such that it results in.\[code\]<xml> <data> <firstname>Peter</firstname> <insertion></insertion> <lastname>John</lastname> <gender>Male</gender> </data></xml>\[/code\]This is my code.\[code\] $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $root = $doc->createElement('data'); $doc->appendChild($root); $data = http://stackoverflow.com/questions/14038660/$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");\[/code\]I am sending this xml as response after creating it. So in client side the lastname node is becoming a subnode of insertion when it is\[code\] <insertion/>\[/code\]
 
Back
Top