zoukankan      html  css  js  c++  java
  • 删除XML文件中的空格

    应要求需要删除xml文件中的空格,制表符等字符。要求双引号和xml的text属性中包含的空格不删除。

      1 bool delSpace(QFile &file, QString path)
      2 
      3 //删除file文件中的空格,双引号以及xml中text属性中的空格不删除。file为目标文件,path为新文件保存的路径及名称。
      4 //操作成功true ,失败返回false
      5 {
      6     QByteArray qfile;
      7     QTextStream in(&qfile);
      8     char ch;        //当前读入的一个字符
      9     char tem;
     10     qint64 position;//文件中存储当前读指针的临时变量
     11 
     12     int ifg = 0;    //双引号的标记,值为1表示进入双引号范围;值为0表示不在双引号范围内
     13     int ifgn = 0;   //找到xml中text属性的标记,值大于0表示找到text属性;值为0表示未找到text属性
     14     int idel = 0;   //进入text属性的标记,值为1表示进入text属性范围;值为0表示不在text属性范围
     15     int ibk = 0;    //退出外层循环标记
     16 
     17 
     18     while(!file.atEnd())
     19     {
     20         file.read(&ch,1);
     21 
     22         if((ifg == 0) && (ifgn == 0))
     23             //当前不再双引号和text属性范围内
     24         {
     25             if(ch == '"')
     26             {
     27                 ifg ++;
     28                 in << ch;
     29 
     30             }
     31             else if((ch == 9 ) || (ch == 10) ||(ch == 13) || (ch == 32) || (ch == 11))
     32             {
     33                 continue;
     34             }
     35             else if(ch == '<')
     36             {
     37                 in << ch;
     38                 position = file.pos(); //保存当前读指针,往后寻找text属性
     39                 while(1)
     40                 {
     41                     file.read(&tem, 1);
     42                     if(tem == '>')
     43                     {
     44                         ifgn ++;
     45                         while(1)
     46                         {
     47                             file.read(&tem, 1);
     48                             if(tem == '<')
     49                             {
     50                                 ifgn ++;
     51                                 file.read(&tem, 1);
     52                                 if(tem == '/')
     53                                 {
     54                                     ifgn ++;     //找到text属性,ifgn = 3
     55                                     ibk = 1;     //退出外层循环
     56                                     break;
     57                                 }
     58                                 else
     59                                 {
     60                                     ifgn = 0;    //未找到text属性,ifgn = 0
     61                                     ibk = 1;     //退出外层循环
     62                                     break;
     63                                 }
     64                             }
     65                             else if(tem == '>')  //寻找text属性失败
     66                             {
     67                                 ifgn = 0;
     68                                 ibk = 1;
     69                                 break;
     70                             }
     71 
     72                         }
     73                         if(ibk == 1)
     74                         {
     75                             file.seek(position);//退出寻找text属性,获取之前保存的读指针
     76                             break;
     77                         }
     78                     }
     79                     else if(tem == '/') //寻找text属性失败
     80                     {
     81                         ifgn = 0;
     82                         file.seek(position);//退出寻找text属性,获取之前保存的读指针
     83                         break;
     84                     }
     85 
     86                 }
     87             }
     88             else
     89             {
     90                 in << ch;
     91             }
     92 
     93         }
     94         else if((ifg > 0) && (ifgn == 0))
     95             //当前在双引号范围内,不再text属性范围内
     96         {
     97             if(ch == '"')
     98             {
     99                 ifg--;
    100                 in << ch;
    101             }
    102             else if(ch == '<')
    103             {
    104                 in << ch;
    105                 position = file.pos();
    106                 while(1)
    107                 {
    108                     file.read(&tem, 1);
    109                     if(tem == '>')
    110                     {
    111                         ifgn ++;
    112                         while(1)
    113                         {
    114                             file.read(&tem, 1);
    115                             if(tem == '<')
    116                             {
    117                                 ifgn ++;
    118                                 file.read(&tem, 1);
    119                                 if(tem == '/')
    120                                 {
    121                                     ifgn ++;     //找到text属性,ifgn = 3
    122                                     ibk = 1;     //退出外层循环
    123                                     break;
    124                                 }
    125                                 else
    126                                 {
    127                                     ibk = 1;    //未找到text属性,ifgn = 0
    128                                     break;      //退出外层循环
    129                                 }
    130                             }
    131                             else if(tem == '>')
    132                             {
    133                                 ifgn = 0;
    134                                 ibk = 1;
    135                                 break;
    136                             }
    137 
    138                         }
    139                         if(ibk == 1)
    140                         {
    141                             file.seek(position);//退出寻找text属性,获取之前保存的读指针
    142                             break;
    143                         }
    144                     }
    145                     else if(tem == '/')
    146                     {
    147                         ifgn = 0;
    148                         file.seek(position);//退出寻找text属性,获取之前保存的读指针
    149                         break;
    150                     }
    151 
    152                 }
    153             }
    154             else
    155             {
    156                 in << ch;
    157             }
    158         }
    159         else if((ifg == 0) && (ifgn > 0))
    160             //当前不在双引号范围内,在text属性范围内
    161         {
    162             if(idel == 0)
    163             {
    164                 if(ch == '"')
    165                 {
    166                     ifg ++;
    167                     in << ch;
    168 
    169                 }
    170                 else if((ch == 9 ) || (ch == 10) ||(ch == 13) || (ch == 32) || (ch == 11))
    171                 {
    172                     continue;
    173                 }
    174                 else if(ch == '>')
    175                 {
    176                     in << ch;
    177                     idel++;
    178                 }
    179                 else
    180                 {
    181                     in << ch;
    182                 }
    183 
    184             }
    185             else if(idel > 0)
    186             {
    187                 if(ch == '<')
    188                 {
    189                     ifgn = 0;
    190                     idel--;
    191                     in << ch;
    192 
    193                 }
    194                 else if(ch == '"')
    195                 {
    196                     ifg ++;
    197                     in << ch;
    198 
    199                 }
    200                 else
    201                 {
    202                     in << ch;
    203                 }
    204             }
    205 
    206 
    207         }
    208         else if((ifg > 0) && (ifgn > 0))
    209             //当前在双引号和text属性范围内
    210         {
    211             if(idel == 0)
    212             {
    213                 if(ch == '"')
    214                 {
    215                     ifg --;
    216                     in << ch;
    217 
    218                 }
    219                 else if(ch == '>')
    220                 {
    221                     in << ch;
    222                     idel++;
    223                 }
    224                 else
    225                 {
    226                     in << ch;
    227                 }
    228 
    229             }
    230             else if(idel > 0)
    231             {
    232                 if(ch == '<')
    233                 {
    234                     ifgn = 0;
    235                     idel--;
    236                     in << ch;
    237 
    238                 }
    239                 else if(ch == '"')
    240                 {
    241                     ifg --;
    242                     in << ch;
    243 
    244                 }
    245                 else
    246                 {
    247                     in << ch;
    248                 }
    249             }
    250         }
    251 
    252     }
    253 
    254     in.flush();
    255     QFile file2(path);
    256     if (file2.open(QFile::ReadWrite|QFile::Truncate))
    257     {
    258         file2.write(qfile);
    259     }
    260     else
    261     {
    262         return false;
    263     }
    264     file2.close();
    265     return true;
    266 }
  • 相关阅读:
    14-定时器
    13-JS中的面向对象
    12-关于DOM操作的相关案例
    11-DOM介绍
    10-关于DOM的事件操作
    09-伪数组 arguments
    08-函数
    07-常用内置对象
    06-流程控制
    05-数据类型转换
  • 原文地址:https://www.cnblogs.com/tyche116/p/8507663.html
Copyright © 2011-2022 走看看