2011-04-19 39 views
1

我的Android應用程序正在調用.NET Web方法,並返回單個字符串。我使用的SAXParser解析一個字符串,如:在我的數據處理程序xml解析包含空格的單個字符串

<string xmlns="http://tempuri.org/">LOCAL NT AUTHORITY\NTLM Authentication Not in role Administrators</string> 

和字符()函數:

 public void characters(char ch[], int start, int length) { 
     String chars = new String(ch, start, length); 
     Log.v("characters", chars); 
     chars = chars.trim(); 

     if (_inItem) { 
      _data = chars; 
     } 
    } 

我的問題是,當我與網絡服務器1測試它,它的作品確定但與Web服務器2它返回空字符串。

使用網絡服務器1,我看到characters()函數只被調用一次,它返回整個字符串「LOCAL NT AUTHORITY \ NTLM身份驗證不在角色管理員」。完善!

但與Web服務器2,charaters()函數被調用多次,它最終返回空字符串..?

04-20 10:09:43.161: VERBOSE/characters(405): LOCAL 
04-20 10:09:43.161: VERBOSE/characters(405): NT AUTHORITY\NTLM Authentication 
04-20 10:09:43.161: VERBOSE/characters(405): Not in role Administrators 
04-20 10:09:43.250: VERBOSE/endElement(405): string=0 

是否需要處理空間?這裏發生了什麼事?

我最終改變字符()函數:

public void characters(char ch[], int start, int length) { 
    String chars = new String(ch, start, length); 

    if (_inItem) { 
     _data += chars; 
    } 
} 
+0

它們是否以不同的字符編碼返回?在開始和結束標記中是否有打印行,以確保XML正確 – Blundell 2011-04-19 22:27:48

+0

在Internet Explorer中,它們返回的是以<?xml version =「1.0」encoding =「utf-8」?> – bob 2011-04-19 22:35:36

回答

0

這是SAX接口是如何定義的,可以有字符多次調用,其並不期望你會得到的所有字符一次呼叫中的單個元素。如果你需要將整個字符串集中在一塊,那麼直到你的實現收集並將它們連接在一起。

+0

開頭的相同xml是的,我現在記得,是的,所以你可以在字符方法中使用StringBuilder來收集字符,然後在endElement()方法中實際構建字符串。 (因爲那時你知道它擁有它們)。 – Blundell 2011-04-19 22:37:45

+0

@superfall你能告訴我爲什麼,在網絡服務器1中,characters()被整個字符串調用一次嗎? – bob 2011-04-19 22:39:32

+0

它只是在數據包/緩衝區邊界等結束的地方。 – superfell 2011-04-19 22:50:18