php - 使用来自 xml 的 php 显示数据

标签 php xml

我想用 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/

相关文章:

android - 在 layout_hint 找不到与给定名称匹配的资源

java - 将上下文传递给非 Activity 类以使用 AssetManager.getAssets()

php - GAE - wordpress 作为 Python 应用程序内的模块

android - ADT 在 Strings.xml 中显示错误

php - Facebook PHP SDK 应用程序将照片上传到页面

php - CakePHP:尝试生成模式时出错

java - java中的xml解析问题

java - Wikipedia Dump 会在 Lucene 中变大吗?

php - DOCX 编码问题

PHP 正则表达式挑战