2012-10-10 100 views
2

我發現了一個XML文件,從NET Web服務的值,像這裏顯示的值獲取XML文件從SOAP響應<BooksResult> XML</ BooksResult>

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <GetDataForRecommendedBooksResponse xmlns="http://tempuri.org/"> 
     <GetDataForRecommendedBooksResult>xml</GetDataForRecommendedBooksResult> 
    </GetDataForRecommendedBooksResponse> 
    </soap:Body> 
</soap:Envelope> 

我reltivly新的Android和我想知道是否有辦法處理這個文件並從中讀取數據?或者我應該只使用一些原始類型而不是xml文件?

回答

1

有很多辦法讓XML內容,我給你我的方式:

您首先需要3個主要方法:

方法1:

public static String getXML(String url){  
      String line = null; 
      Log.d("-----URL STATE -----","Start getXML"); 
      try { 
       URL u = new URL (url); 
       HttpURLConnection huc = (HttpURLConnection) u.openConnection(); 
       huc.setRequestMethod ("GET"); //OR huc.setRequestMethod ("HEAD"); 
       huc.connect() ; 
       int code = huc.getResponseCode() ; 
       System.out.println(code); 
       Log.d("-----URL STATE -----","Checking URL"); 
       if (code==404){ 
        line="Wrong URL"; 
        Log.d("-----URL STATE -----"," Wrong URL"+code); 
       }else{ 
       //------------------- 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
        HttpResponse httpResponse = httpClient.execute(httpPost); 
        HttpEntity httpEntity = httpResponse.getEntity(); 
        line = EntityUtils.toString(httpEntity); 
       } 

      } catch (UnsupportedEncodingException e) { 
       line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
      } catch (MalformedURLException e) { 
       line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
      } catch (IOException e) { 
       line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; 
      } 

      return line; 

    } 

方法2:

public final static Document XMLfromString(String xml){ 

    Document doc = null; 

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
     is.setCharacterStream(new StringReader(xml)); 
     doc = db.parse(is); 

    } catch (ParserConfigurationException e) { 
     System.out.println("XML parse error: " + e.getMessage()); 
     return null; 
    } catch (SAXException e) { 
     System.out.println("Wrong XML file structure: " + e.getMessage()); 
     return null; 
    } catch (IOException e) { 
     System.out.println("I/O exeption: " + e.getMessage()); 
     return null; 
    } 

    return doc; 

} 

方法3:

public static String getValue(Element item, String str) {  
    NodeList n = item.getElementsByTagName(str);   
    return XMLfunctions.getElementValue(n.item(0)); 
} 

使用:

String xml = getXML(url); 
Document doc =XMLfromString(xml); 
NodeList nodes = doc.getElementsByTagName(yourTAG); //TAG you want to get as String 
ArrayList<String> TAGS=new ArrayList<String>(); 
for (int i = 0; i < nodes.getLength(); i++) { 
Element e = (Element) nodes.item(i); 
TAGS.add(getValue(e,yourTAG.toString()); 
} 

現在你可以使用「標籤」

永遠記住'StrictMode.setThreadPolicy' 之前的一切

if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
        .permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 
+0

這很好,但問題在於xml不在Web服務器上。他們有一個在服務器上的Web服務,並通過從我的電腦調用服務,它自動將xml文件下載到我的電腦(它不會在瀏覽器中打開,只需下載爲我的電腦上我的下載文件夾中的文件)在android上獲取它的方法。或者,也許我應該只是改變websercive – Bri6ko

+0

我想你給我一個例子,試試並確切知道發生了什麼.. –

+0

http://oi48.tinypic.com/mre5iw.jpg 我希望這會幫助,我需要捕獲Android設備上的文件或最後嘗試更改Web服務:/ – Bri6ko