2012-01-08 47 views
0

我從一臺服務器接收到360K的數據,並將其轉換爲手機上的XML文件。最終的文件大約是1.3 Mb。後來,我嘗試閱讀這個文件和我的GT-S5660,這個過程運行5(!)小時。雖然它產生了正確的結果,但我不知道是否有辦法優化這個過程。我有一個它的iPhone版本,並在那裏過程完成不到5秒鐘。 這是一個正常的行爲?如果是這樣,SQLIte可以替代嗎? 這裏是我的代碼:檢索XML運行5小時

FileInputStream fileos; 
    try { 
     fileos = openFileInput("TarifsCache" + Constants.VERSION + ".txt"); 
    } catch (FileNotFoundException e) { 
     return; 
    } 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db; 
    try { 
     db = dbf.newDocumentBuilder(); 
    } catch (ParserConfigurationException e) { 
     return; 
    } 
    Document doc; 
    try { 
     doc = db.parse(new InputSource(fileos)); 
    } catch (SAXException e) { 
     return; 
    } catch (IOException e) { 
     return; 
    } 
    doc.getDocumentElement().normalize(); 

    for (int i = 0;; i++) { 

     NodeList nodeList = doc.getElementsByTagName("Cache" + i); 

     Node node = nodeList.item(0); 
     if (node == null) 
      break; 
     Element fstElmnt = (Element) node; 

     NodeList nameList = fstElmnt.getElementsByTagName("area_ref"); 
     Element nameElement = (Element) nameList.item(0); 
     nameList = nameElement.getChildNodes(); 
     tarifread.area_ref = Integer.parseInt(((Node) nameList.item(0)) 
       .getNodeValue()); 

後面有的是其他元素的檢索。

+1

您應該剖析您的代碼並查看哪部分花費最長時間。見http://developer.android.com/guide/developing/debugging/debugging-tracing.html – inazaruk 2012-01-08 16:12:35

+0

我做到了。單獨運行db.parse(...)5分鐘。 normalize需要25秒,第一個doc.getElementsByTagName(「Cache」+ i)需要1分鐘,aso。沒有單一的命令行導致問題 – michaelsmith 2012-01-08 16:25:16

+0

沒有人在這個論壇上可以提供幫助嗎? – michaelsmith 2012-01-09 12:46:25

回答

0


嘗試使用高級解析器解析器像SAX和dnt使用STAX作爲其在android中不支持。