2013-01-13 3 views
1

Continent.txt 파일이 내 res/raw 폴더에 있습니다. 안에는 다음 내용이 들어 있습니다.Jsoup를 사용하여 텍스트 파일 구문 분석

<div class="continents"> 
    <a href="#US">US</a> 
    <a href="#CA">Canada</a> 
    <a href="#EU">Europe</a> 
</div> 

나는 미국, 캐나다, 유럽이 jsoup 사용하여 텍스트 을 구문 분석 할 수 있어요,하지만 난 텍스트 뷰에 표시 할 때, 그들은 한 줄에 표시됩니다. 결과는 다음과 같습니다.

미국 캐나다 유럽

은 내가 OUPUT이처럼되고 싶어요.

미국

캐나다

유럽

이 내 코드입니다.

package com.example.readfile; 


import java.io.InputStream; 
import java.util.ArrayList; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.res.Resources; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    TextView txtContinent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     txtContinent = (TextView) findViewById(R.id.textView1); 

     new MyTask().execute(); 
    } 

    class MyTask extends AsyncTask<Void, Void, ArrayList<String>> { 

     ArrayList<String> arr_linkText = new ArrayList<String>(); 

     @Override 
     protected ArrayList<String> doInBackground(Void... params) { 

      Document doc; 

      try { 
       Resources res = getResources(); 
       InputStream in_s = res.openRawResource(R.raw.continent); 

       byte[] b = new byte[in_s.available()]; 
       in_s.read(b); 

       doc = Jsoup.parse(new String(b)); 
       Element link = doc.select("a").first(); 
       String text = doc.body().text(); 

       arr_linkText.add(text); 

      } catch (Exception e) { 
       // e.printStackTrace(); 
       txtContinent.setText("Error: can't open file."); 
      } 

      return arr_linkText; // << retrun ArrayList from here 
     } 

     @Override 
     protected void onPostExecute(ArrayList<String> result) { 

      for (String temp_result : result) { 

       txtContinent.append(temp_result + "\n"); 
      } 

     } 

    } 

} 

줄 단위로 파일을 읽는 방법을 모르겠지만 다른 사람이 나에게 설명해주기를 바랍니다. 고맙습니다!

답변

1

문서의 전체 본문을 한 번에 가져옵니다. 다음과 같은 대체하기 때문에이 명확하지 않았다 경우

Elements links = doc.select("a"); 
for (Element link : links) { 
    arr_linkText.add(link.text()); 
} 

은, 위의 코드가 의미하는 것처럼, 각각의 요소에 의해 그것을 구문 분석 할 필요가 -

Element link = doc.select("a").first(); 
String text = doc.body().text(); 

arr_linkText.add(text); 
+0

당신은 보스입니다. 도와 주셔서 감사합니다! – Questions

0

android:inputTypetextMultiLine을 포함하도록 설정 했습니까?

+0

이 그것을하지 않았다. 나중에 ListView로 작업 할 수 있으므로 TextView로 작업 했더라도 ListView에는 유용하지 않습니다. – Questions

관련 문제