开发者

Android,SAX parser Problem while reading Html Tags

i want to par开发者_C百科se this text with Sax Parser,the problem is due to Html tags in content tag string buffer is not going to read Html tags can any one suggest me how to do it with Sax Parser,or refer me any of the links that parse Html data with SAX

Android,SAX parser Problem while reading Html Tags


If you can edit the text you provided, simply use CDATA:

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

Then SAX Parser's toString() will return a string like this: Your stuff here with all the <em>HTML</em> tags you can think of.


You can use this method to put CDATA in data (Parameter DATA: Actual Data; TAG: Name of XML tag where CDATA needs to be put.)

 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;
}


An HTML file is not XML conformant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜