Android从XML优化中读取

问题描述:

我正在使用以下代码从存储在我的资产文件夹中的XML文件中读取数据。这是一个简单的XML文件,其结构如下。该文件有大约10个节点(总计约100行)。但在我的Galaxy S2上,这段代码需要10秒钟才能执行。其他方法可以更快吗?谢谢! XML结构:Android从XML优化中读取

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <node> 
     <name>test</name> 
     <picture>test1</picture> 
     <thumbnail>asa</thumbnail> 
     <location>fdsf</location> 
     <description>sdfsd</description> 
    </node> 
    <node> 
     <name>test</name> 
     <picture>test1</picture> 
     <thumbnail>asa</thumbnail> 
     <location>fdsf</location> 
     <description>sdfsd</description> 
    </node> 
    <node> 
     <name>test</name> 
     <picture>test1</picture> 
     <thumbnail>asa</thumbnail> 
     <location>fdsf</location> 
     <description>sdfsd</description> 
    </node> 

    ....... 

</root> 

和代码片段:

String XMLText = "";  
InputStream raw = getApplicationContext().getAssets().open(fileName); 
      InputStreamReader inputStreamReader = new InputStreamReader(raw); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 


      String line = ""; 
      while ((line = bufferedReader.readLine()) != null) { 
       XMLText += line; 
      } 

你应该使用SAX解析器或XmlPullParser。如果要将数据串接/附加到字符串,请使用StringBuilder而不是String类型。