dou91808 2014-01-03 22:06
浏览 33
已采纳

使用HTML格式从PHP创建XML结构

I am creating a system where a user can enter an ingredient and then save its quantity, I use PHP to save it as XML.

It currently saves like this.

 <ingredients>
    <ingredient quantity="2">Apple</ingredient>
    <ingredient quantity="4">Banana</ingredient>
  </ingredients>

Instead i would like to save it like this but i can't figure out how.

  <ingredient>
            <quantity_amount>2</tns:quantity_amount>
            <quantity_name>teaspoons</tns:quantity_name>
            <ingredient_name>baking powder</tns:ingredient_name>
        </ingredient>

for each ingredient added

Here is my PHP and HTML i currently use.

HTML

  <div id="addIngredient">
                <p>
                <input type="text" id="ingredient_1" name="ingredient[]" value="" placeholder="Ingredient"/>
                <input type="text" id="quantity_1" name="quantity[]"  value="" placeholder="Quantity"/>
                <a href="#" id="addNewIngredient">Add</a>
                </p>

                </div>

PHP

// Start for loop, from 0 to ingredientName
for ($i = 0; $i < count($ingredientName); $i++) {
    // Select current item
    $element = $ingredientName[$i];

    // Updated used ingredients list ONLY
    // if the ingredient wasn't added yet
    $temp = search($upIngreds, 'value', $element);
    if (empty($temp)) {
        $upIngreds[] = array('value' => $element, 'label' => $element, 'image' => 'images/products/default.jpg');
    }

    // Select current item quantity
    $qty = $quantity[$i];
    // Create ingredient element and fill with $element
    $ingredient = $xml->createElement('ingredient', $element);
    // Set quantity attiribute to $qty value
    $ingredient->setAttribute('quantity', $qty);
    // Append it to ingredients element
    $ingredients->appendChild($ingredient);
}
  • 写回答

1条回答 默认 最新

  • duankuaiwang2706 2014-01-03 23:21
    关注

    to create a new ingredient according to your desired XML structure:

    // creating the document and its root
    $dom = new DOMDocument('1.0', 'utf-8');
    $root = $dom->createElement('ingredients','');
    $dom->appendChild($root);
    
    // create new ingredient and link it to root 
    $ingredient = $dom->createElement('ingredient','');
    $root->appendChild($ingredient);    
    
    // create children and link them to ingredient
    $q_amount = $dom->createElement('quantity_amount',"1");
    $q_name = $dom->createElement('quantity_name',"spoon");
    $i_name = $dom->createElement('ingredient_name',"PHP");
    $ingredient->appendChild($q_amount);
    $ingredient->appendChild($q_name);
    $ingredient->appendChild($i_name);
    
    echo $dom->saveXML();
    

    see it working: https://eval.in/85654

    Be sure to sanitize user input from your form prior to inserting it into the XML.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘