xml vers une class définie
Bonjour,
Je récupère un XML comme ceci:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8" ?>
<codes>
<nb_ligne>1</nb_ligne>
<code_class>
<code>code1</code>
<liblelle_court>libelle1</libelle_court>
<liblelle_moyen>libelle2</libelle_moyen>
<liblelle_long>libelle3</libelle_long>
<date_debut>20170501</date_debut>
<date_fin>20171231</date_fin>
</code>
</codes> |
J'ai défini une class avec les attributs suivants:
code, libelle_court, libelle_moyen, libelle_long, date_debut, date_fin. Certaine de ces classes n'existe pas systématiqument ex: <liblelle_moyen>libelle2</libelle_moyen> non présent.
Code:
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="UTF-8" ?>
<codes>
<nb_ligne>1</nb_ligne>
<code_class>
<code>code1</code>
<liblelle_court>libelle1</libelle_court>
<liblelle_long>libelle3</libelle_long>
<date_debut>20170501</date_debut>
<date_fin>20171231</date_fin>
</code>
</codes> |
Comment charger le contenu de ce xml dans les attributs de ma classe? Y a-t-il plus simple?
Je peux générer des balises vides (<liblelle_moyen></libelle_moyen>) si besoin.
Pour l'instant je pars pour faire ca, mais c'est moche.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?php
class cp
{
protected $_code;
protected $_libelle_court;
protected $_libelle_moyen;
protected $_libelle_long;
protected $_date_debut;
protected $_date_fin;
public function __construct($code){
$this->_code = $code;
$code_xml=request($this->_code,'cp');
if ($code_xml->nb_ligne = 1) {
if($code_xml->code_class[0]->libelle_court){
$this->_libelle_court = $code_xml->code[0]->libelle_court;
}
if($code_xml->code_class[0]->libelle_moyen){
$this->_libelle_moyen = $code_xml->code[0]->libelle_moyen;
}
if($code_xml->code_class[0]->date_debut){
$this->_date_debut = $code_xml->code[0]->date_debut;
}
if($code_xml->code_class[0]->date_fin){
$this->_date_fin = $code_xml->code[0]->date_fin;
}
} else {
echo "Erreur: CP XML";
}
}
} |