首先,非常初学者 - 试图成为这里的^^。我开发的第一个月,任何建议都将受到赞赏。
我正在尝试开发一个管理面板,使用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');
}
?>
答案 0 :(得分:0)
<?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');