2

我有一个要解析的 XML,如下所示

<feed>
    <feed_id>12941450184d2315fa63d6358242</feed_id>
    <content> <fieldset><table cellpadding='0'  border='0'  cellspacing='0'  style="clear :both"><tr valign='top' ><td width='35' ><a href='http://mypage.rediff.com/android/32868898'  class='space' onmousedown="return enc(this,'http://track.rediff.com/click?url=___http%3A%2F%2Fmypage.rediff.com%2Fandroid%2F32868898___&service=mypage_feeds&clientip=202.137.232.113&pos=0&feed_id=12941450184d2315fa63d6358242&prc_id=32868898&rowid=674061088')" ><div style='width:25px;height:25px;overflow:hidden;'><img src='http://socialimg04.rediff.com/image.php?uid=32868898&type=thumb'  width='25'  vspace='0'  /></div></a></td> <td><span><a href='http://mypage.rediff.com/android/32868898'  class="space" onmousedown="return enc(this,'http://track.rediff.com/click?url=___http%3A%2F%2Fmypage.rediff.com%2Fandroid%2F32868898___&service=mypage_feeds&clientip=202.137.232.113&pos=0&feed_id=12941450184d2315fa63d6358242&prc_id=32868898&rowid=674061088')" >Android </a> </span><span style='color:#000000 !important;'>testing</span><div class='divtext'></div></td></tr><tr><td height='5' ></td></tr></table></fieldset><br/></content>
    <action>status updated</action>
</feed>

标签包含 HTML 内容,其中包含我需要的数据。我正在使用 SAX 解析器。这就是我在做什么

private Timeline timeLine; //Object
private String tempStr;

public void characters(char[] ch, int start, int length)
        throws SAXException {
    tempStr = new String(ch, start, length);
}

public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (localName.equalsIgnoreCase("content")) {
        if (timeLine != null) {
            timeLine.setContent(tempStr);
        }
}

这个逻辑会起作用吗?如果不是,我应该如何使用 SAX 解析器从 XML 中提取嵌入的 HTML 数据。

4

4 回答 4

1

你可以解析html毕竟html也是xml。stackoverflow中有一个类似的链接。你可以试试这个How to parse the html content in android using SAX PARSER

于 2011-01-05T10:07:14.600 回答
1

On start element, if the element is content, your temp Str buffer should be initialized. else if content already started, capture the current start element and its attributes and update that to the temp Str buffer.

On characters, if content is started, add the charecters to the current string buffer.

On end element if content is started, Capture the end node and add to string buffer.

My Assumption:

The xml will have only one content tag.

于 2011-01-05T13:37:20.747 回答
0

我以这种方式找到解决方案:

注意: 在这个解决方案中,我想获取<chapter>标签之间的 html 内容(<chapter> ... html content ... </chapter>)

DefaultHandler handler = new DefaultHandler() {

    boolean chap = false;

    public char[] temp;
    int chapterStart;
    int chapterEnd;

    public void startElement(String uri, String localName,
            String qName, Attributes attributes)
            throws SAXException {

            System.out.println("Start Element :" + qName);

            if (qName.equalsIgnoreCase("chapter")) {
                chap = true;
            }

        }

        public void endElement(String uri, String localName,
            String qName) throws SAXException {

            if (qName.equalsIgnoreCase("chapter")) {
                System.out.println(new String(temp, chapterStart, chapterEnd-chapterStart));

            }
            System.out.println("End Element :" + qName);

        }

        public void characters(char ch[], int start, int length)
                throws SAXException {

            if (chap) {
                temp = ch;
                chapterStart = start;
                chap = false;
            }
            chapterEnd = start + length;

        }

    };

更新:

我的代码有一个错误。因为 DocumentHandler 中 ch[] 的长度在不同的情况下会有所不同!

于 2011-10-01T21:38:31.320 回答
0

如果 html 实际上是 xhtml,您可以使用 SAX 对其进行解析并提取<content>标记的 xhtml 内容,但这并不容易。

您必须让您的处理程序真正响应标签内的所有 xhtml 标签将引发的事件 <content>,并构建类似于 DOM 结构的东西,然后您可以将其序列化回 xml 表单,或者在 - fly 直接写入复制内容的 xml 字符串缓冲区。

如果您修改您的 xml,以便按照如何使用 SAX PARSER 解析 android 中的 html 内容中的建议,将内容标记内的 html 包装在 CDATA 元素中,那么离您的代码不远的东西确实应该可以工作。

但是您不能像您正在做的那样将内容放入方法中的String tempStr变量中。characters您需要有一个startElement方法,在看到<content>标签时为字符串初始化缓冲区,在方法中收集到该缓冲区characters,并将结果放在标签endElement的某处。<content>

于 2011-01-05T12:36:50.683 回答