Tworzę plik xml używając php. Wynikowy kod XML to.Tworzenie węzła XML z wartością pustą w php
<xml>
<data>
<firstname>Peter</firstname>
<insertion>V</insertion>
<lastname>John</lastname>
<gender>Male</gender>
</data>
</xml>
Jednak w przypadku, gdy wartość zerowa jest otrzymany XML jest (wygląd w węźle wprowadzania).
<xml>
<data>
<firstname>Peter</firstname>
<insertion/>
<lastname>John</lastname>
<gender>Male</gender>
</data>
</xml>
Jeśli wartość jest null Chcę xml być tworzone w taki sposób, że powoduje.
<xml>
<data>
<firstname>Peter</firstname>
<insertion></insertion>
<lastname>John</lastname>
<gender>Male</gender>
</data>
</xml>
To jest mój kod.
$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");
Wysyłam ten xml jako odpowiedź po utworzeniu. Tak więc w bok Client lastname węzeł staje się podwęzeł z wkładania gdy jest
<insertion/>
Wydaje się to samo ... to skrótowa alternatywa. Nie wiem, czy przyniosłoby to ten sam wynik w PHP. – codingbiz
@codingbiz: czy mógłbyś być bardziej konkretny? nie można zrozumieć, co masz na myśli – Avinash
zarówno i są takie same. –
Zeina