Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display data with php from xml

Tags:

php

xml

I want to get and display some values from a xml code with php. So, when I remove the balise "base", I can display what I want, but I have to put this balise to have several "table".
This is my xml :

<?xml version="1.0" encoding="UTF-8"?>


<base>  
<table nom="analyse">
    <champs>
        <nom>id_analyse</nom>
        <decription>Identifiant de l'analyse</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>id_zp</nom>
        <decription>Identifiant de la zone de prélèvement</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>id_essai</nom>
        <decription>Identifiant de l'essai</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>id_traitement</nom>
        <decription>Identifiant du traitement</decription>
        <type>Char</type>
        <type_entree>Facultatif</type_entree>
    </champs>

    <champs>
        <nom>code_traitement</nom>
        <decription>Code_traitement</decription>
        <type>Char</type>
        <type_entree>Facultatif</type_entree>
    </champs>
</table>

<table name="bloc">
    <champs>
        <nom>id_bloc</nom>
        <decription>Identifiant du bloc</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>bloc</nom>
        <decription>Nom du bloc</decription>
        <type>Char</type>
        <type_entree>Facultatif</type_entree>
    </champs>

    <champs>
        <nom>id_essai</nom>
        <decription>Identifiant de l'essai</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>
</table>
</base>

And this is my php code :

  <?php
  $fichier = 'arbre_xml_BDD.xml';
  $xml = simplexml_load_file($fichier);

  foreach($xml as $champs){
      echo $champs->nom.'</br>';
      echo $champs->decription.'</br>';
      echo $champs->type.'</br>';
      echo $champs->type_entree.'</br>';
      echo'</br>';

  }
  ?>

Help me please !

like image 650
Erlaunis Avatar asked Nov 18 '25 08:11

Erlaunis


1 Answers

You need to do another foreach statement, your first one will only go around the table and not the champs, please see below:

<?php
$fichier = 'arbre_xml_BDD.xml';
$xml = simplexml_load_file($fichier);

foreach ($xml as $table) { // Loop around each <table> element
    foreach ($table as $champs) { // Loop around each sub child (champs) of <table> parent
        echo $champs->nom.'</br>';
        echo $champs->decription.'</br>';
        echo $champs->type.'</br>';
        echo $champs->type_entree.'</br>';
        echo'</br>';
    }
}
?>
like image 168
Danny Broadbent Avatar answered Nov 19 '25 23:11

Danny Broadbent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!