Home > OS >  Insert value in xml file when field match
Insert value in xml file when field match

Time:07-30

is it possible to copy the "MASSIMALE" value of the massimali.xml file into the "MASSIMALE"field of the esercizi.xml file when "UTENTE" and "ESERCIZIO" match using php?

Thank You

esercizi.xml

<?xml version="1.0"?>
<sessioni>
  <sessione position="2">
    <index>2</index>
    <nome_sessione>w1.1</nome_sessione>
    <utente>USER1</utente>
    <esercizio>BENCH PRESS</esercizio>
    <rep_sessione>1</rep_sessione>
    <percent_sessione>50</percent_sessione>
    <massimale/>
  </sessione>
  <sessione position="3">
    <index>3</index>
    <nome_sessione>w1.1</nome_sessione>
    <utente>USER2</utente>
    <esercizio>SQUAT</esercizio>
    <rep_sessione>1</rep_sessione>
    <percent_sessione>50</percent_sessione>
    <massimale/>
  </sessione>
</sessioni>

massimali.xml

<?xml version="1.0"?>
<massimali>
  
  <atleta position="2">
    <id/>
    <utente>USER2</utente>
    <esercizio>BENCH PRESS</esercizio>
    <massimale>50</massimale>
    <data>2022-07-29</data>
  </atleta>
  <atleta position="3">
    <id/>
    <utente>USER1</utente>
    <esercizio>BENCH PRESS</esercizio>
    <massimale>98</massimale>
    <data>2022-07-29</data>
  </atleta>
  
</massimali>

CodePudding user response:

Since you're dealing with two xml files, you should be using an xml parser with xpath, like this:

#load the files
$ese = simplexml_load_file("esercizi.xml");
$mas = simplexml_load_file("massimali.xml");

#load each file into xml
$masdoc = new SimpleXMLElement($mas);
$esedoc = new SimpleXMLElement($ese);

#get the athletes in the first file
$masatletas = $masdoc->xpath('//atleta');    

#now loop through them:
foreach($masatletas as $masatleta) {
 $masute =  $masatleta->xpath('.//utente')[0];
 $masese = $masatleta->xpath('.//esercizio')[0];
 $masmas = $masatleta->xpath('.//massimale')[0];

 #this is the critical directive: look for the each artist in the other file
 #and check to see if the credentials are the same:     
 $target = $esedoc->xpath("//sessione[.//utente= '{$masute}'][.//esercizio= '{$masese}']");

 if (count($target) == 1) {
           #if so, change the value to fit that of its equivalent in the first:
           $newmas = $target[0]->xpath('.//massimale/text()')[0];
           $newmas[0] = $masmas;
           }
}
echo $esedoc->asXml();

Output:

<?xml version="1.0"?>
<sessioni>
      <sessione position="2">
        <index>2</index>
        <nome_sessione>w1.1</nome_sessione>
        <utente>USER1</utente>
        <esercizio>BENCH PRESS</esercizio>
        <rep_sessione>1</rep_sessione>
        <percent_sessione>50</percent_sessione>
        <massimale>98</massimale>
      </sessione>
      <sessione position="3">
        <index>3</index>
        <nome_sessione>w1.1</nome_sessione>
        <utente>USER2</utente>
        <esercizio>SQUAT</esercizio>
        <rep_sessione>1</rep_sessione>
        <percent_sessione>50</percent_sessione>
        <massimale/>
      </sessione>
    </sessioni>

If you haven't dealt with xml and xpath before, this will be a lot to digest, but you have to start sometime...

CodePudding user response:

This work fine

$xmlFile = file_get_contents('files/esercizi_in_sessione.xml');
$xml_ese= str_replace(['&lt;','&gt;'],['<','>'], $xmlFile);

$xmlFile1 = file_get_contents('files/massimali.xml');
$xml_mas= str_replace(['&lt;','&gt;'],['<','>'], $xmlFile1);




#load the files
#$ese = simplexml_load_file("files/esercizi_in_sessione.xml");
#$mas = simplexml_load_file("massimali.xml");

#load each file into xml
$masdoc = new SimpleXMLElement($xml_mas);
$esedoc = new SimpleXMLElement($xml_ese);

#get the athletes in the first file
$masatletas = $masdoc->xpath('//atleta');    

#now loop through them:

foreach($masatletas as $masatleta) {
  
 $masute =  $masatleta->xpath('.//utente')[0];
 $masese = $masatleta->xpath('.//esercizio')[0];
 $masmas = $masatleta->xpath('.//massimale')[0];

 #this is the critical directive: look for the each artist in the other file
 #and check to see if the credentials are the same:     
 $target = $esedoc->xpath("//sessione[.//utente= '{$masute}'][.//esercizio= '{$masese}']");

 if (count($target) == 1) {
           #if so, change the value to fit that of its equivalent in the first:
           $newmas = $target[0]->xpath('.//massimale')[0];
           $newmas[0] = $masmas;
           }
}




echo $esedoc->asXml('files/prova.xml');
  • Related