SAX解析器在swing工作线程内部没有完全工作

时间:2015-07-23 09:49:04

标签: java xml parsing sax swingworker

我尝试了一切但却无法正常工作:

@Override
protected Void doInBackground()
    throws Exception
{
    // This code uses the online stream and parse the xml.
    class SaxHandler extends DefaultHandler
    {
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException
        {
            System.out.println(qName);

            switch (qName) {
                case "requiredName":
                // Code to create new node
                break ;
            }
        }

        @Override
        public void characters(char[] ch, int start, int length)
            throws SAXException
        {
            content = String.copyValueOf(ch, start, length).trim();
        }

        @Override
        public void endElement(String uri, String localName, String qName) 
            throws SAXException
        {
            System.out.println("---" + qName + "---");

            switch (qName) {
                case "closing tag":
                    publish(object);
            }
        }
    }

    URL url = new URL("http://feeds.feedburner.com/blogspot/infogate");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.connect();
    InputStream stream = con.getInputStream();
    SAXParserFactory spfac = SAXParserFactory.newInstance();

    SAXParser parser = spfac.newSAXParser();
    SaxHandler handler = new SaxHandler();
    System.out.println("I am here");
    parser.parse(stream, handler);
}

出于测试目的,我只希望解析器在解析XML时写出它遇到的所有标记名称。但它只会写一些起始标记,然后它不会解析更多。

如果您转到XML Page并查看其来源,那么您将能够看到这些标记。它只解析起始行的一些标记,直到第一个<title>标记。

更新:解析代码在单独的类中正常工作,但在Swing工作线程的doInBackground()方法中编写时无效。

我的应用程序中的其他所有内容都准备好了,我仍然坚持这一部分。

0 个答案:

没有答案
相关问题