[인터돌™] 공부 해보자!! 열심히~~~

반응형
안드로이드 2.1 환경에서 테스트해본 XmlPullParser 샘플 코드 입니다. xml은 웹상에 있는 것을 받아오고 (HttpClient 이용) 단순히 xml 태그 안에 있는 값을 출력합니다. 태그 안에 들어있는 속성값을 출력하는 부분도 들어있습니다.

package com.interdol.globalhost;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class ContactServicePullParser extends BaseService implements
        IBaseService {

    private StringBuffer tempStrBuffer = new StringBuffer();

    private ArrayList contactList = new ArrayList();

    @Override
    public ArrayList getVOArray(String type) {
        // TODO Auto-generated method stub
        if (type == "contactForm") {
            return getContactsList();
        }
        return null;
    }

    private ArrayList getContactsList() {
//        System.out.println("ContactServicEPullParser is called");

        // 서버로부터 xml을 불러옴
        String request = "http://192.168.1.10:9090/J2EE/xml/ContactList.xml";
        String resultString = "";

        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(request);

        try {

            // Send GET request
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }
            InputStream rstream = null;

            // Get the response body
            rstream = method.getResponseBodyAsStream();

            // Process the response from Yahoo! Web Services
            BufferedReader br = new BufferedReader(new InputStreamReader(rstream));

            String line;
            while ((line = br.readLine()) != null) {
                resultString += line;
            }
            br.close();
        } catch (Exception e) {
            // TODO: handle exception
        }

        // XmlPullParser 구문 시작
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(resultString));
//            System.out.println(resultString);
            // xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if (eventType == XmlPullParser.END_DOCUMENT) {
                    System.out.println("End document");
                }else if (eventType == XmlPullParser.START_TAG) {
//                    System.out.println("Start tag " + xpp.getName());
                    if (xpp.getName().equals("fullName")) {
                        System.out.println(xpp.getName() + " : " + xpp.nextText());
                    }else if (xpp.getName().equals("contacts")) {
                        // totalCount
                        System.out.println("Here!!!! : " + xpp.getName() + " : " + xpp.getAttributeValue(null, "totalCount"));
                    }

                } else if (eventType == XmlPullParser.END_TAG) {
                    System.out.println("End tag " + xpp.getName());
                } else if (eventType == XmlPullParser.TEXT) {
                    String tempStr = "Text : " + xpp.getText();
                }

                eventType = xpp.next();
            }
        } catch (Exception e) {

        }

        return contactList;
    }
}




※ 참고
Android에서 XML문서의 파싱을 위해 제공하는 3가지 방법 : http://www.androidpub.com/49492



이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band