douzhoubing2805 2014-10-19 10:27
浏览 30
已采纳

在PHP节点值和元素中创建的Xml

I do not understand these node value things at all i am trying to replicate an xml design in php but
having quite a bit of trouble the file i am trying to reproduce through php is.

   <items>
        <item>
              <id></id>
              <name></name>
              <price></price>
              <quantity></quantity>
              <description></description>
              <qonhold></qonhold>
              <qsold></qsold>
        </item>
     </items>

And the PHP file to recreate this is almost all done

  $dom = new DOMDocument("1.0");

  // create root element
  $root = $dom->createElement("Items");
  $dom->appendChild($root);
  $dom->formatOutput=true;

  // create child element
  $item = $dom->createElement("item");
  $dom->appendChild($item);

  // create text node
  $id = $dom->createElement("id");
  $root->appendChild($id);

  $name = $dom->createElement("name");
  $root->appendChild($name);


  $price = $dom->createElement("price");
  $root->appendChild($price);

  $quantity = $dom->createElement("quantity");
  $root->appendChild($quantity);

  $description = $dom->createElement("description");
  $root->appendChild($description);


  $qonhold = $dom->createElement("qonhold");
  $root->appendChild($qonhold);


  $qsold = $dom->createElement("qsold");
  $root->appendChild($qsold);

The problem i am having is its saving it all under "items" being the root.. but i can not get everything id, name, price, quantity, description, qonhold, qsold to save under just "item" which is saved under "items

  • 写回答

1条回答 默认 最新

  • douluo2930 2014-10-19 10:29
    关注

    You should use ->appendChild() on the item node created, not the root which is <items>:

    // create child element
    $item = $dom->createElement("item");
    $dom->appendChild($item);
    
    // create text node
    $id = $dom->createElement("id");
    $item->appendChild($id); // item->appendChild not $root->appendChild
    

    Should look like this:

    $dom = new DOMDocument("1.0");
    
    // create root element
    $root = $dom->createElement("Items");
    $dom->appendChild($root);
    $dom->formatOutput=true;
    
    // create child element
    $item = $dom->createElement("item");
    $root->appendChild($item); // append to `<Items>`
    
    // create text node
    $id = $dom->createElement("id");
    $item->appendChild($id); // append to `<item>`
    
    $name = $dom->createElement("name");
    $item->appendChild($name); // append to `<item>`
    
    
    $price = $dom->createElement("price");
    $item->appendChild($price); // append to `<item>`
    
    $quantity = $dom->createElement("quantity");
    $item->appendChild($quantity); // append to `<item>`
    
    $description = $dom->createElement("description");
    $item->appendChild($description); // append to `<item>`
    
    
    $qonhold = $dom->createElement("qonhold");
    $item->appendChild($qonhold); // append to `<item>`
    
    
    $qsold = $dom->createElement("qsold");
    $item->appendChild($qsold); // append to `<item>`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题