简体   繁体   English

Android中的SAX解析

[英]SAX parsing in android

I have xml file that looks like this: 我有看起来像这样的xml文件:

 <ns:retrieveLastResponse>
 <ns:return xsi:type="ax21:MinusEntry">
 <ax21:entrydate>2010-07-02T17:29:35.492+02:00</ax21:entrydate>
 <ax21:minus>true</ax21:minus> 
 <ax21:password>SECRET</ax21:password> 
 <ax21:text>Some text</ax21:text>
<ax21:username>John Doe</ax21:username>
 </ns:return>
</ns:retrieveLastResponse>

I'm trying to parse it with sax like this: 我正在尝试像这样用sax解析它:

try {

        URL rssUrl = new URL("ADDRESS OF WS THAT RETURNS XML FILE"); 
        SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        XMLReader myXMLReader = mySAXParser.getXMLReader();
        RSSHandler myRSSHandler = new RSSHandler();
        myXMLReader.setContentHandler(myRSSHandler);
        InputSource myInputSource = new InputSource(rssUrl.openStream());
        //InputSource myInputSource = new InputSource(rssUrl.openConnection().getInputStream());
        myXMLReader.parse(myInputSource);

        //result.setText(myRSSHandler.getMessages().get(0).getDesc());
        result.setText(Integer.toString(myRSSHandler.getMessages().size()));

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result.setText("Cannot connect RSS!");
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result.setText("Cannot connect RSS!");
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result.setText("Cannot connect RSS!");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        result.setText("Cannot connect RSS!");
    }


}

private class RSSHandler extends DefaultHandler
{

    private List<Message> messages;     
    private Message currentMessage;
    private StringBuilder builder;


    public List<Message> getMessages(){
        return this.messages;
    }

    @Override
    public void startDocument() throws SAXException {

        messages = new ArrayList<Message>();
        builder = new StringBuilder();
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub

    }

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

        super.startElement(uri, localName, qName, attributes);

        // TODO Auto-generated method stub
        if (localName.equalsIgnoreCase("ns:return")){
            Log.d("XML","Zacetek xmla");
            this.currentMessage = new Message();
        }

    }

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

        super.endElement(uri, localName, qName);

        if (this.currentMessage != null){
            if (localName.equalsIgnoreCase("ax21:entrydate")){
                Log.d("XML","Vstop v datum");
                currentMessage.setDate(builder.toString());

            } else if (localName.equalsIgnoreCase("ax21:minus")){
                currentMessage.setMin(builder.toString());
            } else if (localName.equalsIgnoreCase("ax21:text")){
                currentMessage.setDesc(builder.toString());
            } else if (localName.equalsIgnoreCase("ax21:username")){
                currentMessage.setUser(builder.toString());
            }
            else if(localName.equalsIgnoreCase("ns:return")){
                messages.add(currentMessage);               
            }
            builder.setLength(0);             
        }

        }



    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        super.characters(ch, start, length);

        builder.append(ch, start, length);
    }

But my list of messages is always empty, my log messages that are inside endElement and startElement are never displayed. 但是我的消息列表始终为空,在endElement和startElement内的日志消息从不显示。 How can i check if the program gets the xml file at all or am i parsing it incorectly? 如何检查程序是否完全获取了xml文件,或者我无法正确解析它?

The problem was in endElement and in startElement if clauses. 问题出在endElement和startElement if子句中。 I should be chekcking for "if (localName.equalsIgnoreCase("minus"))" instead of "if (localName.equalsIgnoreCase("ax21:minus"))" 我应该检查“ if(localName.equalsIgnoreCase(“ minus”))“,而不是” if(localName.equalsIgnoreCase(“ ax21:minus”))“

I also added this line as suggested: mySAXParserFactory.setNamespaceAware(true); 我还根据建议添加了以下行:mySAXParserFactory.setNamespaceAware(true);

If that's your XML file, it's malformed (you're using namespaces ns and ax21 without declaring them). 如果那是您的XML文件,则格式错误(您使用的是nsax21名称空间,而未声明它们)。 Have you elided the XML example? 您是否删除了XML示例?

Haven't tried SAX parsing myself yet, but don't you think setNamespaceAware(true) might be a good idea? 还没有尝试过SAX自己解析,但是您认为setNamespaceAware(true)可能不是一个好主意吗?

As for checking, have you considered adding a unit test project? 至于检查,您是否考虑过添加单元测试项目? Have you tried stepping through the code in Eclipse/ADT? 您是否尝试过逐步浏览Eclipse / ADT中的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM