【问题标题】:How to split XML elements - php如何拆分 XML 元素 - php
【发布时间】:2017-06-03 20:38:07
【问题描述】:

首先,非常初级的开发者尝试在这里 ^^。我在开发的第一个月,任何建议都将不胜感激。

我正在尝试开发一个管理面板,使用 PHP DOM 将数据从 HTML 表单插入 XML 文件。

这是代码的结果。

<element>
<name> Name 1 </name>
<quantity> Quantity 1</quantity>
<name> Name 2</name>
<quantity> Quantity 2</quantity> 
<name> Name 3</name>
<quantity> Quantity 3</quantity>   
</element>

这是我想要的结果。

 <list>
   <ing>
    <name> Name </name>
    <quantity> Quantity </quantity>
   </ing>

   <ing>
    <name> Name </name>
    <quantity> Quantity </quantity> 
   </ing>

    <ing>
     <name> Name </name>
     <quantity> Quantity </quantity>   
    </ing>
  </list>

最后是我的 php 代码:我正在使用 DOM。

   <?php
            if(isset($_REQUEST['ok'])){
              $xml = new DOMDocument ('1.0','UTF-8');

              $xml ->load('asd.xml');
              $rootTag = $xml ->getElementsByTagName('Settings')->item(0);


              $dataTag = $xml ->createElement('RecipeInfo');
              $dataTag->setAttribute("id",3);    //





              $sumTag = $xml ->createElement('RecipeSummary');
              $listTag = $xml ->createElement('RecipeIngredientsList');
              $ingTag = $xml ->createElement('RecipeIngredient');



    $aTag = $xml ->createElement('RecipeTitle',$_REQUEST['RecipeTitle']);
    $bTag = $xml ->createElement('RecipePicture',$_REQUEST['RecipePicture']);
    $cTag = $xml ->createElement('RecipeSummaryOrigin',$_REQUEST['RecipeSummaryOrigin']);
    $dTag = $xml ->createElement('RecipeSummaryPreparationTime',$_REQUEST['RecipeSummaryPreparationTime']);
    $eTag = $xml ->createElement('RecipeSummaryCookingTime',$_REQUEST['RecipeSummaryCookingTime']);
    $fTag = $xml ->createElement('RecipeSummaryPortions',$_REQUEST['RecipeSummaryPortions']);
    $gTag = $xml ->createElement('RecipeSummaryCalories',$_REQUEST['RecipeSummaryCalories']);
    $hTag = $xml ->createElement('RecipeSummaryDescription',$_REQUEST['RecipeSummaryDescription']);

    $jTag = $xml ->createElement('RecipeIngredientsName',$_REQUEST['RecipeIngredientsName']);
    $kTag = $xml ->createElement('RecipeIngredientsQuantity',$_REQUEST['RecipeIngredientsQuantity']);


$lTag = $xml ->createElement('iRecipeIngredientsName',$_REQUEST['iRecipeIngredientsName']);
    $mTag = $xml ->createElement('RecipeIngredientsQuantity',$_REQUEST['RecipeIngredientsQuantity']);

    $nTag = $xml ->createElement('uRecipeIngredientsName',$_REQUEST['uRecipeIngredientsName']);
    $oTag = $xml ->createElement('RecipeIngredientsQuantity',$_REQUEST['RecipeIngredientsQuantity']);

    $pTag = $xml ->createElement('dRecipeIngredientsName',$_REQUEST['dRecipeIngredientsName']);
    $rTag = $xml ->createElement('RecipeIngredientsQuantity',$_REQUEST['RecipeIngredientsQuantity']);

    $sTag = $xml ->createElement('bRecipeIngredientsName',$_REQUEST['bRecipeIngredientsName']);
    $tTag = $xml ->createElement('RecipeIngredientsQuantity',$_REQUEST['RecipeIngredientsQuantity']);


              $dataTag->appendChild($aTag);
              $dataTag->appendChild($bTag);
              $dataTag->appendChild($sumTag);
              $dataTag->appendChild($listTag);
              $listTag->appendChild($ingTag);

              //Recipe Summary Child'ları
              $sumTag->appendChild($cTag);
              $sumTag->appendChild($dTag);
              $sumTag->appendChild($eTag);
              $sumTag->appendChild($fTag);
              $sumTag->appendChild($gTag);
              $sumTag->appendChild($hTag);


              $ingTag->appendChild($jTag);
              $ingTag->appendChild($kTag);

              $ingTag->appendChild($lTag);
              $ingTag->appendChild($mTag);

              $ingTag->appendChild($nTag);
              $ingTag->appendChild($oTag);

              $ingTag->appendChild($pTag);
              $ingTag->appendChild($rTag);

               $ingTag->appendChild($sTag);
               $ingTag->appendChild($tTag);




              $rootTag->appendChild($dataTag);
              $xml->save('asd.xml');
              }

      ?>

【问题讨论】:

  • 你写的结果不可能来自这段代码......(除非结果与文件asd.xml中的结果完全相同,如果是这样,那么代码什么都不做)

标签: php html xml dom domdocument


【解决方案1】:
<?php

$source = [['name' => 'name 1', 'quantity' => 'quantity 1'],
         ['name' => 'name 2', 'quantity' => 'quantity 2'],
         ['name' => 'name 3', 'quantity' => 'quantity 3']];

$xml = new DOMDocument ('1.0','UTF-8');

$list = $xml->createElement('list');

foreach($source as $data){
    $ing = $xml->createElement('ing');
    $ing->appendChild($xml->createElement('name', $data['name']));
    $ing->appendChild($xml->createElement('quantity', $data['quantity']));
    $list->appendChild($ing);
}

$xml->appendChild($list);

$xml->save('out.xml');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多