47staticfiledemo2는 raw폴더 생성 하고 product.xml생성을 해야함

                                       46은 raw -> words.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent" >
	<TextView
		android:id="@+id/selection"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
	/>
	<ListView
		android:id="@android:id/list"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		android:drawSelectorOnTop="false"
	/>
</LinearLayout>


<words>
	<word value="lorem" />
	<word value="ipsum" />
	<word value="dolor" />
	<word value="sit" />
	<word value="amet" />
	<word value="consectetuer" />
	<word value="adipiscing" />
	<word value="elit" />
	<word value="morbi" />
	<word value="vel" />
	<word value="ligula" />
	<word value="vitae" />
	<word value="arcu" />
	<word value="aliquet" />
	<word value="mollis" />
	<word value="etiam" />
	<word value="vel" />
	<word value="erat" />
	<word value="placerat" />
	<word value="ante" />
	<word value="porttitor" />
	<word value="sodales" />
	<word value="pellentesque" />
	<word value="augue" />
	<word value="purus" />
</words>


package om.commonsware.android.files2;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class StaticFileDemo extends ListActivity {
	TextView selection;
	ArrayList<string> items = new ArrayList<string>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        selection = (TextView)findViewById(R.id.selection);
        
        try{
        	//raw\words.xml 을 읽어들여서 InputStream으로 반환
        	InputStream in = getResources().openRawResource(R.raw.words);
        	//Dom 파서 생성
        	DocumentBuilder builder =DocumentBuilderFactory
        			.newInstance()
        			.newDocumentBuilder();
        	//Dom파서가 전달된 InputStream을 읽어 들이고 파싱해서 메모리상에 Dom 트리 생성
        	Document doc = builder.parse(in);
        	//word Element명을 검색해서 같은 레벨에 Element들을 nodeList로 반환
        	NodeList words = doc.getElementsByTagName("word");
        	
        	for(int i=0;i<words.getlength();i++){ 
        		items.add(((element)words.item(i)).getattribute("value"));="" 
        				}="" in.close();="" }catch(throwable="" t){
        					="" toast.maketext(this,"exception="" :="" "+
        				t.tostring(),2000).show();="" setlistadapter(new="" arrayadapter
        				<string="">(this,android.R.layout.simple_list_item_1,items));
        	}
    }
    
    public void onListItemClick(ListView parent,View v, int position,long id){
    	selection.setText(items.get(position).toString());
    }
}






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/parse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Parse XML" />

	<EditText
	    android:id="@+id/result"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:scrollbars="vertical"
	    android:textSize="20sp"
	    android:gravity="top" />
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<order>
	<item>Mouse</item>
	<item>Computer</item>
	<item>Android</item>
	<item>IPhone</item>

</order>
package com.commonsware.android.files3;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class StaticFileDemo2 extends Activity implements View.OnClickListener {
	EditText mResult;
	Document doc;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mResult = (EditText) findViewById(R.id.result);

		Button btn = (Button) findViewById(R.id.parse);
		btn.setOnClickListener(this);

		try {
			//raw\product.xml를 읽어들여 InputStream 반환
			InputStream in = getResources().openRawResource(R.raw.product);
			//Dom 파서 생성
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			//product.xml 을 읽어들인 InputStream을 파싱해서
			//메모리에 DOM 트리 생성
			doc = builder.parse(in);
		} catch (Exception e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}

	public void onClick(View v) {
		StringBuffer sb = new StringBuffer();
		NodeList items = doc.getElementsByTagName("item");

		sb.append("주문 항목\n");
		for (int i = 0; i < items.getLength(); i++) {
			String itemName = items.item(i).getFirstChild().getNodeValue();
			sb.append((i + 1) + " : " + itemName + "\n");
		}
		mResult.setText(sb.toString());
	}
}



'Android > 기본' 카테고리의 다른 글

Read JSON  (0) 2012.04.28
XML Resource  (0) 2012.04.28
Read Write File (자동 저장할때 사용)  (0) 2012.04.28
Access Web  (0) 2012.04.28
NetWork Wifi , 3G  (0) 2012.04.28

+ Recent posts