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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法