2012-10-24 49 views
1

我正在解析來自下面顯示的Web服務的數據:我只能解析兩個狀態值爲bal的數值,並且接下來是空值指針例外。 我能夠只檢索狀態和lname.Can任何人都可以告訴我這是什麼問題。我得到 status = 1這是正確的,bal = Finn,其餘爲空。使用SAX解析從XML中檢索數據僅檢索第一個和最後一個值

XML:

<root> 
<response> 
<status>1</status> 
<bal>3.69 EUR</bal> 
<cur>EUR</cur> 
<acc>2974342749</acc> 
<fname>Welcome To</fname> 
<lname>Finn</lname> 
</response> 
</root> 

代碼:

主要活動

btn.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       new Communicator().execute(); 
      } 
     }); 
} 

public class Communicator extends AsyncTask<String, String, SitesList1> 
{ 
    @Override 
     protected SitesList1 doInBackground(String... params) { 
      try { 
        SAXParserFactory spf = SAXParserFactory.newInstance(); 
        SAXParser sp = spf.newSAXParser(); 
        XMLReader xr = sp.getXMLReader(); 

        URL sourceUrl = new URL("XXXXX![enter image description here][2]"); 
        MyXMLHandler1 myXMLHandler1 = new MyXMLHandler1(); 
        xr.setContentHandler(myXMLHandler1); 
        xr.parse(new InputSource(sourceUrl.openStream())); 
      } catch (Exception e) { 
       System.out.println("XML Pasing Excpetion = " + e); 
      } 

      sitesList1 = MyXMLHandler1.sitesList1; 

      return sitesList1; 
     } 

     @Override 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
     } 

     @SuppressWarnings("deprecation") 
     @Override 
     protected void onPostExecute(SitesList1 result) 
     { 
       status = sitesList1.getStatus().toString(); 
       System.out.println("status"+status); 
       bal = sitesList1.getBal().toString(); 
       System.out.println("bal"+bal); 
       cur = sitesList1.getCur().toString(); 
       System.out.println("cur"+cur); 
       acc = sitesList1.getAcc().toString(); 
       System.out.println("acc"+acc); 
       fname = sitesList1.getFname().toString(); 
       System.out.println("fname"+fname); 
       lname = sitesList1.getLname().toString(); 
       System.out.println("lname"+lname); 

     } 
    } 

處理程序:

公共類MyXMLHandler1擴展了DefaultHandler {

Boolean currentElement = false; 
String currentValue = null; 
public static SitesList1 sitesList1 = null; 

public SitesList1 getSitesList1() { 
    return sitesList1; 
} 

public void setSitesList1(SitesList1 sitesList1) { 
    this.sitesList1 = sitesList1; 
} 

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

    currentElement = true; 

    if (localName.equals("root")) { 
     sitesList1 = new SitesList1(); 
    } 
} 

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

    currentElement = false; 

    if (localName.equalsIgnoreCase("status")) 
     sitesList1.setStatus(currentValue); 
    else if (localName.equalsIgnoreCase("bal")) 
     sitesList1.setBal(currentValue); 
    else if (localName.equalsIgnoreCase("cur")) 
     sitesList1.setBal(currentValue); 
    else if (localName.equalsIgnoreCase("acc")) 
     sitesList1.setBal(currentValue); 
    else if (localName.equalsIgnoreCase("fname")) 
     sitesList1.setBal(currentValue); 
    else if (localName.equalsIgnoreCase("lname")) 
     sitesList1.setBal(currentValue); 

} 

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

    if (currentElement) { 
     currentValue = new String(ch, start, length); 
     currentElement = false; 
    } 

} 

}

二傳手消氣:

公共類SitesList1 {

private String status; 
private String bal; 
private String cur; 
private String acc; 
private String fname; 
private String lname; 
public String getStatus() { 
    return status; 
} 
public void setStatus(String status) { 
    this.status = status; 
} 
public String getBal() { 
    return bal; 
} 
public void setBal(String bal) { 
    this.bal = bal; 
} 
public String getCur() { 
    return cur; 
} 
public void setCur(String cur) { 
    this.cur = cur; 
} 
public String getAcc() { 
    return acc; 
} 
public void setAcc(String acc) { 
    this.acc = acc; 
} 
public String getFname() { 
    return fname; 
} 
public void setFname(String fname) { 
    this.fname = fname; 
} 
public String getLname() { 
    return lname; 
} 
public void setLname(String lname) { 
    this.lname = lname; 
} 

}

回答

0

的問題是在你characters方法。您沒有正確重置currentValue。嘗試這樣的事情,而不是:

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

public void startDocument() throws SAXException { 
super.startDocument(); 
currentValue= new StringBuilder(); 
} 

然後,在你endElement方法,你可以這樣做:

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

    currentElement = false; 

    if (localName.equalsIgnoreCase("status")) 
     sitesList1.setStatus(currentValue.toString()); 
    else if (localName.equalsIgnoreCase("bal")) 
     sitesList1.setBal(currentValue.toString()); 
    else if (localName.equalsIgnoreCase("cur")) 
     sitesList1.setCur(currentValue.toString()); 
    else if (localName.equalsIgnoreCase("acc")) 
     sitesList1.setAcc(currentValue.toString()); 
    else if (localName.equalsIgnoreCase("fname")) 
     sitesList1.setFname(currentValue.toString()); 
    else if (localName.equalsIgnoreCase("lname")) 
     sitesList1.setLname(currentValue.toString()); 

    //flush out the StringBuilder so you can start again 
    curentValue.setLength(0); 

} 
+0

它並沒有奏效。同樣的結果。 – user1744952

+0

我剛剛注意到,在if/else語句的每一種情況下,您都會調用「setBal」。你需要調用適當的setter。 – Chris

相關問題