XML 파서 (p400)


안드로이드에서는 3가지 XML 파서 방법이 지원된다. : DOM파서, SAX파서, XmlPullParser



1. xml로 데이터를 만든다.

경로는 res에 raw라는 폴더를 만든 뒤 그 안에서 파일 생성-student.xml이란 이름으로 만든다.


<person>

    <student>

        <name>홍길동</name>
<age>30</age>
<address>서울</address>
</student>
<student>
<name>이순신</name>
<age>40</age>
<address>전라도</address>
</student>

<student>
<name>강감찬</name>
<age>50</age>
<address>경상도</address>
</student>

</person>



2. 레이아웃



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.dom.MainActivity">


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ListView
android:background="#5a87cf"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

</RelativeLayout>




3. 메인 액티비티


package com.progdv.dom;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

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

public class MainActivity extends AppCompatActivity {

ListView listView;

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

listView = (ListView)findViewById(R.id.listView);
String[] data = xmlParsing();

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}

public String[] xmlParsing(){

String[] data = null;

try {
InputStream is = getResources().openRawResource(R.raw.student);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);

NodeList studentList = doc.getElementsByTagName("student");
NodeList nameList = doc.getElementsByTagName("name");
NodeList ageList = doc.getElementsByTagName("age");
NodeList addressList = doc.getElementsByTagName("address");

data = new String[studentList.getLength()];

for(int i = 0; i < studentList.getLength(); i++) {
String name = nameList.item(i).getFirstChild().getNodeValue();
String age = ageList.item(i).getFirstChild().getNodeValue();
String addr = addressList.item(i).getFirstChild().getNodeValue();

data[i] = name + " " + age + " " + addr;
}

} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}
return data;
}


}

웹에서의 자바스크립트와 같은 방법으로 데이터를 넘겨줌.





















Posted by netyhobby
,