Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd an element into xml file using php
    primarykey
    data
    text
    <p>I have a problem with php and xml files: I want to add an element into xml file. I've wrote this code and it works:</p> <pre><code>&lt;?php $file = 'clients.xml'; $doc = new DOMDocument(); $doc-&gt;load($file); $get_elms = $doc-&gt;getElementsByTagName("Client"); $nr_elms = $get_elms-&gt;length; $node = $get_elms-&gt;item($nr_elms-1); $element = $node-&gt;nodeName; if($element=='Client') { $idfinal=intval($node-&gt;getAttribute('id'))+1; $client = $doc-&gt;createElement('Client'); $node-&gt;parentNode-&gt;appendChild($client); $client-&gt;setAttribute('id',$idfinal); $client-&gt;setAttribute('name','Any name'); $client-&gt;setAttribute('cnp','12345'); $client-&gt;setAttribute('adress','adress'); $client-&gt;setAttribute('nb','123'); $client-&gt;setAttribute('amount','1000'); $client-&gt;setAttribute('type','Any type'); } if($doc-&gt;save('clients.xml')) { echo htmlentities($doc-&gt;saveXML()); } ?&gt; </code></pre> <p>If I put exactly the same code into a class, into function addClient it doesn't work because it doesn't recognize my xml file. I've tried echo $get_elms->length; and it returns me 0 and my variable $element is empty so it does not enter in that "if". I need to use this class because I must do more operations like delete and update and I must send some parameters to those functions. Can anyone help me and tell me what am I doing wrong? </p> <p>My class looks like this:</p> <pre><code>class xmlClientMappers { public function adaugaClient(array $row) { $name = $row['name']; $adress = $row['adress']; $cnp = $row['cnp']; $nb = $row['nb']; $amount = $row['amount']; $type = $row['type']; $file = 'clients.xml'; $doc = new DOMDocument(); $doc-&gt;load($file); $get_elms = $doc-&gt;getElementsByTagName("*"); $nr_elms = $get_elms-&gt;length; echo "The nomber is ".$nr_elms; $node = $get_elms-&gt;item($nr_elms-1); $element = $node-&gt;nodeName; echo "&lt;br/&gt;Here should be the name of the last element".$element; if($element=='Client') { $idfinal=intval($node-&gt;getAttribute('id'))+1; echo " id-ul e ".$idfinal; $client = $doc-&gt;createElement('Client'); $node-&gt;parentNode-&gt;appendChild($client); $client-&gt;setAttribute('id',$idfinal); $client-&gt;setAttribute('name',$name); $client-&gt;setAttribute('cnp',$cnp); $client-&gt;setAttribute('adress',$adress); $client-&gt;setAttribute('nb',$nb); $client-&gt;setAttribute('amount',$amount); $client-&gt;setAttribute('type',$type); } if($doc-&gt;save('clienti.xml')) { echo htmlentities($doc-&gt;saveXML()); else echo "It is wrong!"; } } } ?&gt; </code></pre> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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