dsds33222 2014-12-29 12:42
浏览 154
已采纳

如何从大型XML文件中删除所有注释? [关闭]

How can I remove all comments from a large XML file?

I have a large file XML and I want to thin it and I want to remove all the comments. The file has a size of over 200 mb and it takes a lot to parse the file and query something.

Code for parse is :

<?php

$dom    = new DOMDocument();
$xpath  = new DOMXPath($dom);
$reader = new XMLReader();
$reader->open('http://www.bookingassist.ro/test/HotelsPro.xml');

while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Hotel') {
        $node = $dom->importNode($reader->expand(), true);
        $dom->appendChild($node);
        $result = $xpath->evaluate('string(self::Hotel[HotelCode = "'.$hotelCodes[3].'"]/HotelImages/ImageURL[1])', $node);
        $dom->removeChild($node);
        if ($result) {
            echo $result;

        }
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dongzhi9032 2014-12-29 12:50
    关注

    Assuming Xslt is an option, you can use a modified version of the identity transform which will project nothing for any matched comment:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="comment()"/>
    
    </xsl:stylesheet>
    

    Fiddle here

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

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突