【问题标题】:xml parsing using SAX parser in java在java中使用SAX解析器解析xml
【发布时间】:2015-11-17 05:32:20
【问题描述】:

我正在尝试解析 rss xml,但在解析描述时卡住了,因为我的程序在遇到 (') 时会停止解析描述内容。

解析xml的代码:

public class RSSAX {

String channel_title="";

public void displayRSS()
{

    try {

        SAXParserFactory spf =  SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        sp.parse("http://www.ronkaplansbaseballbookshelf.com/feed/podcast/", new RSSHandler());


    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("Messge is "+e.getMessage());
    }

}

private class RSSHandler extends DefaultHandler
{
    private boolean isItem = false;
    private String tagName=""; 

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        this.tagName= qName;
        if(qName.equals("item"))
        {
            this.isItem=true;
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
         this.tagName="";
         if(qName.equals("item"))
         {
             System.out.println("========================");
             this.isItem=false;
         }


    }

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

        if(this.isItem)
        {
            //System.out.println("tagname is "+this.tagName);
            if(this.tagName.equals("title"))
            {
                System.out.println("title is "+(new String(ch,start,length)));
                this.tagName="";
            }
            else if(this.tagName.equals("link"))
            {
                System.out.println("link is "+(new String(ch,start,length)));
                this.tagName="";
            }
            else if(this.tagName.equals("description"))
            {
                String test=(new String(ch,start,length)).replaceAll("\\<.*?>","");
                test=StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(test));
                System.out.println("description is "+test);
                this.tagName="";
            }
            else if(this.tagName.equals("comments"))
            {
                System.out.println("comment link is "+(new String(ch,start,length)));
                this.tagName="";
            }
            else if(this.tagName.equals("pubDate"))
            {
                System.out.println("pubDate is "+(new String(ch,start,length)));
                this.tagName="";
            }
            else if(this.tagName.equals("category"))
            {
                System.out.println("Category is "+(new String(ch,start,length)));
                this.tagName="";
            }
            else if(this.tagName.equals("content:encoded"))
            {
                System.out.println("content:encoded is "+(new String(ch,start,length)));
                //this.tagName="";
            }

        }

    }

}



输出:

标题是书架对话:菲利普·邦迪
链接是http://www.ronkaplansbaseballbookshelf.com/2015/08/04/the-bookshelf-conversation-filip-bondy/
pubDate 是 2015 年 8 月 4 日星期二 14:31:45 +0000
评论链接是http://www.ronkaplansbaseballbookshelf.com/2015/08/04/the-bookshelf-conversation-filip-bondy/#comments
类别是 2015 年的标题 类别是作者简介/Ron Kaplan 的采访

description is My New Jersey 陆地人和资深体育作家 Filip Bondy 为全国消遣历史上最著名的游戏之一制作了一本有趣的书。随时随地

当遇到 there's..

时,它会停止解析描述

【问题讨论】:

  • 有什么异常?

标签: java xml parsing saxparser


【解决方案1】:

SAX 解析器可以任意方式分解文本节点,并通过多次调用 characters() 方法来传递内容。重新组装零件是您的工作。

【讨论】:

  • 你能帮我建议在代码中进行所需的更新以执行所需的操作,即获取整个描述。
  • 任何有关 SAX 的教程都会解释此信息。我自己在各种 XML 书籍中编写了一些内容。我不会为你再写一篇。
【解决方案2】:

您可以使用STAXParser,在此强制 XMLStreamReader 返回单个字符串,您可以包括:

factory.setProperty("javax.xml.stream.isCoalescing", true);

这有助于作为一个字符串返回,参考XMLStreamReade.next() Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 2011-05-31
    • 2011-04-30
    • 2017-08-19
    • 2017-07-27
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多