PHP - XML - Creating XML

Use PHP's DOM extension to create a DOM tree and append nodes to it:

  1. <?php
  2. // initialize DOM object
  3. $xml = new DOMDocument("1.0");
  4. // add root node <listing>
  5. $root = $xml->createElement("list");
  6. $xml->appendChild($root);
  7. // add element <description> to root
  8. $desc = $xml->createElement("description");
  9. $root->appendChild($desc);
  10. // add <description> content
  11. $desc->appendChild($xml->createTextNode("Sam's Shopping List"));
  12. // add comment
  13. $root->appendChild($xml->createComment("item list follows"));
  14. // add <item> child element
  15. // add quantities as attributes
  16. $items = $xml->createElement("items");
  17. $root->appendChild($items);
  18. $item = $xml->createElement("item");
  19. $items->appendChild($item);
  20. $item->appendChild($xml->createTextNode("eggs"));
  21. $item->setAttribute("units", 3);
  22. unset($item);
  23. $item = $xml->createElement("item");
  24. $items->appendChild($item);
  25. $item->appendChild($xml->createTextNode("salt"));
  26. $item->setAttribute("units""100 gm");
  27. // add CDATA block
  28. $items->appendChild($xml->createCDATASection("You can't make↩
  29. an omelette without breaking eggs"));
  30. // add PI
  31. $root->appendChild($xml->createProcessingInstruction(↩
  32. "xml-dummy-pi""shop('now')"));
  33. // display final tree as HTML…
  34. $xml->formatOutput = true;
  35. echo "<xmp>" . $xml->saveXML() . "</xmp>";
  36. // …or write it to a file as XML
  37. $xml->save("list.xml"or die("ERROR: Could not write to file");
  38. ?>

Comments

PHP's SimpleXML extension does not support node creation, so this task is better handled with PHP's DOM extension, which comes with a wide array of methods designed to help you design an XML document instance dynamically. To get started, create an instance of the DOMDocument class, and then use its createElement() method to create element objects. These element objects may then be attached to a parent node by calling the parent node object's appendChild() method. The process is illustrated in the previous listing.

Of course, an XML document is much more than just elements—which is why the DOM extension also offers createTextNode(), createCDATASection(), createProcessingInstruction(), and createComment() methods to attach text, CDATA blocks, PIs (Processing Instructions), and comments to the DOM tree respectively. Attributes for an element are set by calling the corresponding element object's setAttribute() method with appropriate parameters.

Once the tree has been generated, it may be retrieved as a string with the primary DOMDocument object's saveXML() method, or written to a file with the object's save() method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值