PHP - XML - Collapsing Empty XML Elements

 Use PEAR's XML_Util class:

 

  1. <?php
  2. $xmlData =<<< END
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7.  <head></head>
  8.  <body>
  9.   <div>Content here.</div>
  10.   <p></p>
  11.   <div>Content here.</div>
  12.  </body>
  13. </html>
  14. END;
  15. // include class
  16. include "XML/Util.php";
  17. // collapse empty tags and print
  18. echo XML_Util::collapseEmptyTags($xmlData);
  19. ?>

Comments

For elements that have no content, such as <name></name>, the XML specification suggests the use of empty-element tags, such as <name />. Available from http://pear.php.net/package/XML_Util, PEAR's XML_Util class provides an automated way to accomplish this change via its collapseEmptyTags() method. One of the most common uses for this method is illustrated in the previous listing: replace HTML's <p></p> sequence with the "better" <p /> sequence.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值