2010-07-08 26 views
1

解析我的XML文件看起來像這樣:SAX android系統

<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> 

我想用這樣的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); 
    } 

但是我的郵件列表總是空,我的日誌消息在endElement和startElement中都不會顯示。我如何檢查程序是否獲取了xml文件或者我是否會不情願地解析它?

+1

hmmmmm .....有sax的機器人?嗯...... – 2010-07-08 08:50:10

+0

它在那裏http://developer.android.com/intl/de/reference/android/sax/package-summary.html – Pentium10 2010-07-08 08:57:41

+0

您是否嘗試過將Log.d行移出if語句?或者在字符功能中輸入一些日誌來打印字符,以檢查您是否正在閱讀某些內容。 – 2010-07-08 15:24:28

回答

1

問題出在endElement和startElement if子句中。我應該爲chekcking 「如果(localName.equalsIgnoreCase(」 而非減 「))」, 「如果(localName.equalsIgnoreCase(」 AX21:減 「))」

我還添加了此行的建議:mySAXParserFactory。 setNamespaceAware(真);

0

如果這是您的XML文件,它的格式不正確(您正在使用命名空間nsax21而未聲明它們)。你是否忽略了XML示例?

還沒有嘗試SAX解析我自己,但你不覺得setNamespaceAware(true)可能是個好主意嗎?

至於檢查,你有沒有考慮添加單元測試項目?您是否嘗試過在Eclipse/ADT中的代碼?

+0

對不起,我忽略了xml文件的開頭,它看起來像這樣: 2010-07-08T00:27:38.520 + 02:00 SECRET TSLA IPO,相當失敗 John Doe DixieFlatline 2010-07-08 09:02:08

+0

我輸入startDocument和endDocument,但沒有別的?! – DixieFlatline 2010-07-08 09:27:59