我想用 php 从 xml 代码中获取并显示一些值。
所以,当我移除应答器“底座”时,我可以显示我想要的东西,但我必须把这个应答器放在几个“表”上。
这是我的 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>
这是我的 PHP 代码:
<?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>';
}
?>
请帮帮我!
最佳答案
你需要再做一个 foreach 语句,你的第一个只会绕过 table 而不是冠军,请看下面:
<?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>';
}
}
?>
关于php - 使用来自 xml 的 php 显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241819/