简体   繁体   中英

PHP - Edit XML file that contains a string

I wanna change a line in a XML file that contains a code obtained via POST.

I am searching for a way to find the file on XML files' directory that contains the code, for then change the line.

Can someone help me?

My PHP code:

$files = scandir('xml/.'); // List files
foreach($files as $file) { 
  if($file == '.' || $file == '..') continue; 
  $xml = simplexml_load_file("xml/".$file);
  $result = $xml->xpath("pedido"); 
  if($result[3]->numpedido == $dilma) {
   echo "It exists!";
  }
}

/* My XML Looks like this
<pedido>
  <cliente>João</cliente>
  <mesa>34</mesa>
  <hora>13:01:10</hora>
  <numpedido>6780110</numpedido>
  <lista>Baiao, Frango, Porco, e Macaxeira</lista>
  <status>Aberto</status>
</pedido>
*/

Edit: The error that I am getting is:

Notice: Undefined offset: 3 in /var/www/html/restaurante/cmae/rita.php on line 43

Notice: Trying to get property of non-object in /var/www/html/restaurante/cmae/rita.php on line 43

$result = $xml->xpath("pedido"); will not return any result. You need to change your xpath to $result = $xml->xpath("/pedido"); . That way you will have an array with simple XML elements (the handling of result needs to be different).

if (isset($result[0]) && isset($result[0]->numpedido) && $result[0]->numpedido == $dilma) {
    echo 'exists';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM