【发布时间】:2011-04-08 05:04:09
【问题描述】:
我想用 Sax Parser 解析这个文本,问题是由于内容标签字符串缓冲区中的 Html 标签不会读取 Html 标签,谁能建议我如何用 Sax Parser 来做,或参考我使用 SAX 解析 Html 数据的任何链接
【问题讨论】:
我想用 Sax Parser 解析这个文本,问题是由于内容标签字符串缓冲区中的 Html 标签不会读取 Html 标签,谁能建议我如何用 Sax Parser 来做,或参考我使用 SAX 解析 Html 数据的任何链接
【问题讨论】:
如果您可以编辑您提供的文本,只需使用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 <em>HTML</em> tags you can think of.
【讨论】:
您可以使用此方法将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;
}
【讨论】:
HTML 文件不符合 XML。
【讨论】: