0

this is my php code: after execute this php, the data display bottom on this file. i want it to just add in one staff element.

$name = $_POST['sName'];
$ic = $_POST['sIC'];
$email = $_POST['sEmail'];
$address = $_POST['sAddress'];
$nophone = $_POST['sPhone'];

$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;


//loading the existing XML file
$dom->load('phone_XML.xml');

write new xml node in same file php:

$phone = $dom->documentElement;
$staff = $dom->documentElement;
// appendChild() can be combined with the create*()
//$staff = $phone->appendChild($dom->createElement("staff"));

// you missed the `s` element node
$s = $staff->appendChild($dom->createElement('s'));
$s->setAttribute('id', 'S002Doe');

$s
->appendChild($dom->createElement('sName'))
->appendChild($dom->createTextNode($name));
$s
->appendChild($dom->createElement('sIC'))
->appendChild($dom->createTextNode($ic));
$s
->appendChild($dom->createElement('sEmail'))
->appendChild($dom->createTextNode($email));
$s
->appendChild($dom->createElement('sAddress'))
->appendChild($dom->createTextNode($address));
$s
->appendChild($dom->createElement('sPhone'))
->appendChild($dom->createTextNode($nophone));

save on the same file:

$dom->save('phone_XML.xml');
echo "<script language='JavaScript'>alert('Add item succesful');</script>";
echo "<script language='JavaScript'>window.location = 'index.php';</script>";
?>

output should be like this :

<phone>
<staff>
<s id="S001Akmal">
  <sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
  <sIC>940228-10-6101</sIC>
  <sEmail>muhdnurakmal@velocity.net.my</sEmail>
  <sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
  <sPhone>012-3456789</sPhone>
</s>
<s id="S002Atikah">
  <sName> Nur Atikah Binti Rohizad </sName>
  <sIC>940421-14-6236</sIC>
  <sEmail>nuratikah@gmail.com</sEmail>
  <sAddress>No 24, Taman Saujana Impian, 43000 Kajang</sAddress>
  <sPhone>013-6752800 </sPhone>
</s>
</staff>
</phone>
Akmal
  • 13
  • 5

2 Answers2

0

Here probrably different question but have same meaning

PHP XML how to output nice format

good luck

Community
  • 1
  • 1
Evinn
  • 153
  • 1
  • 11
0

You're loading an existing XML. This will include some text nodes with whitespaces (line breaks, spaces for indentation). DOM recognizes them in the serialization a tries to preserve the original XML (including the whitespace nodes) over reformatting. The DOMDocument::$preserveWhiteSpace property allows it to remove the whitespace node while parsing.

Other points:

  • You missed the s element node.
  • The phone element node is the document element it is available in a property of the DOMDocument object.
  • appendChild() can be combined with create*() calls. It returns the appended node.
  • Add text nodes. Do not use the second argument of createElement(), it has a broken escaping.

Put together:

$xml = <<<'XML'
<phone>
<staff>
    <s id="S001Akmal">
        <sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
        <sIC>940228-10-6101</sIC>
        <sEmail>muhdnurakmal@velocity.net.my</sEmail>
        <sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
        <sPhone>012-3456789</sPhone>
    </s>
</staff>
</phone>
XML;

$document = new DOMDocument();
$document->formatOutput = TRUE;
$document->preserveWhiteSpace = FALSE;
$document->loadXml($xml);

$phone = $document->documentElement;
// appendChild() can be combined with the create*()
$staff = $phone->appendChild($document->createElement("staff"));

// you missed the `s` element node
$s = $staff->appendChild($document->createElement('s'));
$s->setAttribute('id', 'S002Doe');

$s
  ->appendChild($document->createElement('sName'))
  ->appendChild($document->createTextNode("John Doe"));
$s
  ->appendChild($document->createElement('sIC'))
  ->appendChild($document->createTextNode("123"));
$s
  ->appendChild($document->createElement('sEmail'))
  ->appendChild($document->createTextNode("john@example.tld"));
$s
  ->appendChild($document->createElement('sAddress'))
  ->appendChild($document->createTextNode("somewhere"));
$s
  ->appendChild($document->createElement('sPhone'))
  ->appendChild($document->createTextNode("456"));

echo $document->saveXml();

Output:

<?xml version="1.0"?>
<phone>
  <staff>
    <s id="S001Akmal">
      <sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
      <sIC>940228-10-6101</sIC>
      <sEmail>muhdnurakmal@velocity.net.my</sEmail>
      <sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
      <sPhone>012-3456789</sPhone>
    </s>
  </staff>
  <staff>
    <s id="S002Doe">
      <sName>John Doe</sName>
      <sIC>123</sIC>
      <sEmail>john@example.tld</sEmail>
      <sAddress>somewhere</sAddress>
      <sPhone>456</sPhone>
    </s>
  </staff>
</phone>

Hint: If posting questions, try to build complete examples that can be run without external dependencies.

ThW
  • 19,120
  • 3
  • 22
  • 44