【问题标题】:Android,SAX parser Problem while reading Html TagsAndroid,SAX 解析器在读取 Html 标签时出现问题
【发布时间】:2011-04-08 05:04:09
【问题描述】:

我想用 Sax Parser 解析这个文本,问题是由于内容标签字符串缓冲区中的 Html 标签不会读取 Html 标签,谁能建议我如何用 Sax Parser 来做,或参考我使用 SAX 解析 Html 数据的任何链接

【问题讨论】:

    标签: android android-emulator


    【解决方案1】:

    如果您可以编辑您提供的文本,只需使用CDATA

    <content><![CDATA[Your stuff here with all the <em>HTML</em> tags you can think of.]]></content>
    

    然后 SAX Parser 的 toString() 将返回一个像这样的字符串:Your stuff here with all the &lt;em&gt;HTML&lt;/em&gt; tags you can think of.

    【讨论】:

      【解决方案2】:

      您可以使用此方法将CDATA放入数据中(参数DATA:实际数据;TAG:需要放入CDATA的XML标签的名称。)

       public static final String putCDATA(String data, String tag) {
          if(data == null || data.length() <= 0 || tag == null || tag.length() <= 0) {
              return null;
          }
      
          String newData = "";
      
          while(true) {
              int firstIndex = data.indexOf("<" + tag + ">");
              firstIndex = firstIndex + new String("<" + tag + ">").length() - 1;
      
              int lastIndex = data.indexOf("</" + tag + ">");
      
              if(firstIndex == -1 || lastIndex == -1) {
                  break;
              }
      
              String tagValue = data.substring(firstIndex + 1, lastIndex);
              tagValue = "<![CDATA[" + tagValue + "]]>";
      
              newData += data.substring(0,firstIndex + 1);
              newData += tagValue;
              newData += data.substring(lastIndex, lastIndex + new String("<" + tag + ">").length() + 1);
      
              data = data.substring(lastIndex + new String("<" + tag + ">").length() + 1, data.length());
          }
      
          newData += data;
      
          System.out.print("FORMATED: " + "\n" + newData);
          return newData;
      }
      

      【讨论】:

        【解决方案3】:

        HTML 文件不符合 XML。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-09
          • 1970-01-01
          • 1970-01-01
          • 2012-05-26
          • 1970-01-01
          • 2011-03-11
          • 2011-06-05
          • 2012-12-26
          相关资源
          最近更新 更多