用HTML表单/ PHP编写分层XML

时间:2018-02-28 19:28:38

标签: php xml

我正在开发一个项目,我正在寻求提供一个HTML表单,用户可以在表单输入中输入值,然后在提交时将其添加到XML文件中。

到目前为止,我已经找到了如何执行此操作的示例,但仅限于XML数据的一个级别,例如具有“name”输入的表单,该表单提交到XML文件,如下所示:

    <meals><type>breakfast</type></meals>

或者也许是这样:

    <meals>
       <meal>
          <type>breakfast</type>
       </meal>
    </meals>

我正在寻求使用表单来编写额外级别的XML节点内容,例如:

    <meals>
       <meal>
          <type>breakfast</type>
          <ingredients>
             <ing>eggs</ing>
          </ingredients>
       </meal>
       <meal>
          <type>dinner</type>
          <ingredients>
             <ing>pork chop</ing>
          </ingredients>
       </meal>
    </meals>

我有一个提供这些元素输入的表单,以及一个将它们写入XML文档的PHP脚本,但我不知道如何迭代它们的父元素各自的嵌套数组。当我提交上面的数据时,我得到的XML如下所示:

    <meals>
       <meal>
          <type>breakfast</type>
          <ingredients>
             <ing>eggs</ing>
             <ing>pork chop</ing>
          </ingredients>
       </meal>
       <meal>
          <type>dinner</type>
          <ingredients>
             <ing>eggs</ing>
             <ing>pork chop</ing>
          </ingredients>
       </meal>
    </meals>

换句话说,我的PHP脚本创建了一系列的膳食和一系列成分,但我想知道是否有办法创建嵌套的膳食&gt;成分或膳食[i]成分。

编辑以添加HTML表单和PHP脚本的代码:

HTML表单:

    <form>
    <input name="done" value="done" type="submit">
    <fieldset name="meal">
       type: <input name="type[]" type="text">
       <br>
       Ingredients
       <fieldset name="ingredients">
          ing. name: <input name="ingName[]" type="text">
       </fieldset>
     </fieldset>
    <fieldset name="meal">
       type: <input name="type[]" type="text">
       <br>
       Ingredients
       <fieldset name="ingredients">
          ing. name: <input name="ingName[]" type="text">
       </fieldset>
     </fieldset>
    </form>

有JS允许添加额外的膳食和成分输入。

这是PHP脚本:         

    if(isset($_REQUEST['done']))
    {$xml = new DOMDocument("1.0","UTF-8");
     $xml->load("groceries4.xml");
     $rootTag=$xml->getElementsByTagName("groceries")->item(0);

     $mealTypes=$_REQUEST['type'];
        foreach($mealTypes as $mt)
        {$mealTag=$xml->createElement("meal");
         $mealType=$xml->createElement("type",$mt);
         $mealTag->appendChild($mealType);
         $ingrsTag=$xml->createElement("ingredients");
         $mealTag->appendChild($ingrsTag);
            $mealIngs=$_REQUEST['ingName'];
            foreach($mealIngs as $mi)
            {$ingTag=$xml->createElement("ing",$mi);
             $ingrsTag->appendChild($ingTag);};
         $rootTag->appendChild($mealTag);}; 


     $xml->save("groceries4.xml");
     }
    ?>

1 个答案:

答案 0 :(得分:1)

许多开发人员忽略了他们如何命名输入字段,特别是涉及related information时。

在这种特殊情况下,它可以使您在构建XML时更轻松。

<强> HTML

<form method="post">
    <!-- meal #1 -->
    <div class="meal">
        <h1>Meal #1</h1>
        <h2>Type</h2>
        <input type="text" name="meals[0][type]" value="m1 t" />
        <div class="ingredients">
            <h2>Ingredients</h2>
            <div class="ingredient">
                <h3>Ingredient #1</h3>
                <input type="text" name="meals[0][ingredients][0][inc]" value="m1 ing1" />
            </div>
            <div class="ingredient">
                <h3>Ingredient #2</h3>
                <input type="text" name="meals[0][ingredients][1][inc]" value="m1 ing2" />
            </div>
        </div>
    </div>
    <!-- meal #2 -->
    <div class="meal">
        <h1>Meal #2</h1>
        <h2>Type</h2>
        <input type="text" name="meals[1][type]" value="m2 t" />
        <div class="ingredients">
            <h2>Ingredients</h2>
            <div class="ingredient">
                <h3>Ingredient #1</h3>
                <input type="text" name="meals[1][ingredients][0][inc]" value="m2 ing1" />
            </div>
            <div class="ingredient">
                <h3>Ingredient #2</h3>
                <input type="text" name="meals[1][ingredients][1][inc]" value="m2 ing2" />
            </div>
        </div>
    </div>
    <!-- keep going -->
    <input type="submit" value="Save" />
</form>

提交后,它可以更容易循环

<强> PHP

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $doc = new DOMDocument('1.0', 'UTF-8');
    $xmlEl = $doc->createElement("xml");
    $xmlEl = $doc->appendChild($xmlEl);
    // add meals
    if (isset($_POST['meals'])) {
        $mealsEl = $doc->createElement("meals");
        $mealsEl = $xmlEl->appendChild($mealsEl);

        foreach ($_POST['meals'] as $meal) {
            // add meal
            $mealEl = $doc->createElement("meal");
            $mealEl = $mealsEl->appendChild($mealEl);
            // add meal type
            if (isset($meal['type'])) {
                $mealEl->appendChild($doc->createElement("type", $meal['type']));
            }
            // add meal ingredients
            if (isset($meal['ingredients'])) {
                $ingredientsEl = $doc->createElement("ingredients");
                $ingredientsEl = $mealEl->appendChild($ingredientsEl);

                foreach ($meal['ingredients'] as $ingredient) {
                    // add ingredient
                    $ingredientEl = $doc->createElement("ingredient");
                    $ingredientEl = $ingredientsEl->appendChild($ingredientEl);
                    // add inc
                    if (isset($ingredient['inc'])) {
                        $ingredientEl->appendChild($doc->createElement("inc", $ingredient['inc']));
                    }
                }
            }
        }
    }
    $doc->save("meals.xml");
}
?>

虽然如果你足够聪明,你可能会递归地构建XML,因为某处有一个模式 - 不幸的是,我不是。

此答案基于此answer