首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SAX解析问题

SAX解析问题
EN

Stack Overflow用户
提问于 2012-03-16 13:58:08
回答 1查看 308关注 0票数 1

我如何像这样解析XML?

代码语言:javascript
复制
<rss version="0.92">
<channel>
<title>MyTitle</title>
<link>http://myurl.com</link>
<description>MyDescription</description>
<lastBuildDate>SomeDate</lastBuildDate>
<docs>http://someurl.com</docs>
<language>SomeLanguage</language>

<item>
    <title>TitleOne</title>
    <description><![CDATA[Some text.]]></description>
    <link>http://linktoarticle.com</link>
</item>

<item>
    <title>TitleTwo</title>
    <description><![CDATA[Some other text.]]></description>
    <link>http://linktoanotherarticle.com</link>
</item>

</channel>
</rss>

请大家帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-16 14:03:02

尝尝这个

代码语言:javascript
复制
public class ExampleHandler extends DefaultHandler {

private Channel channel;
private Items items;
private Item item;
private boolean inItem = false;

private StringBuilder content;

public ExampleHandler() {
    items = new Items();
    content = new StringBuilder();
}

public void startElement(String uri, String localName, String qName, 
        Attributes atts) throws SAXException {
    content = new StringBuilder();
    if(localName.equalsIgnoreCase("channel")) {
        channel = new Channel();
    } else if(localName.equalsIgnoreCase("item")) {
        inItem = true;
        item = new Item();
    }
}

public void endElement(String uri, String localName, String qName) 
        throws SAXException {
    if(localName.equalsIgnoreCase("title")) {
        if(inItem) {
            item.setTitle(content.toString());
        } else {
            channel.setTitle(content.toString());
        }
    } else if(localName.equalsIgnoreCase("link")) {
        if(inItem) {
            item.setLink(content.toString());
        } else {
            channel.setLink(content.toString());
        }
    } else if(localName.equalsIgnoreCase("description")) {
        if(inItem) {
            item.setDescription(content.toString());
        } else {
            channel.setDescription(content.toString());
        }
    } else if(localName.equalsIgnoreCase("lastBuildDate")) {
        channel.setLastBuildDate(content.toString());
    } else if(localName.equalsIgnoreCase("docs")) {
        channel.setDocs(content.toString());
    } else if(localName.equalsIgnoreCase("language")) {
        channel.setLanguage(content.toString());
    } else if(localName.equalsIgnoreCase("item")) {
        inItem = false;
        items.add(item);
    } else if(localName.equalsIgnoreCase("channel")) {
        channel.setItems(items);
    }
}

public void characters(char[] ch, int start, int length) 
        throws SAXException {
    content.append(ch, start, length);
}

public void endDocument() throws SAXException {
    // you can do something here for example send
    // the Channel object somewhere or whatever.
}

}

其中Item、Items和Channel是getter setter类...

要了解更多信息,请访问此问题How to parse XML using the SAX parser

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9732628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档