본문 바로가기

JAVA/Android

네트워크(Network) : DOM 파서

 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    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=".MainActivity" >

 

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="연결 요청" />

 

    <ScrollView

        android:id="@+id/scrollView1"

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

 

        <TextView

            android:id="@+id/textView1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />

 

    </ScrollView>

 

</LinearLayout>

 

 

 

 

MainActivity.java

public class MainActivity extends Activity {

 

         @Override

         protected void onCreate(Bundle savedInstanceState) {

                  super.onCreate(savedInstanceState);

                  setContentView(R.layout.activity_main);

                 

                  findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

                          

                           @Override

                           public void onClick(View v) {

                                   // TODO Auto-generated method stub

                                   String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><order><item>Mouse<item>Mouse</item></item><item>KeyBoard</item><item>HDD</item></order>";

                                   readXML(xml);

                                  

                           }

                  });

         }

        

         private void readXML(String xml){

                  try {

                           //DOM parse 만드는

                           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

                           DocumentBuilder builder = factory.newDocumentBuilder();

                          

                           //xml파일을 String 시킴

                           InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));

                          

                           Document document = builder.parse(is);

                          

                           //root tag 가져옴================================

                           Element order = document.getDocumentElement();

                          

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

textView1.setText("");                          

textView1.append("루트 엘리먼트 : " + order.getTagName() + "\n------------------------------\n");

                          

                           //첫번째 방법

                           /*//order 첫번째 엘리먼트================================

                           Element item1 = (Element)order.getFirstChild();               //첫번째 자식태그을 가져옴 : item

                           textView1.append("item1(첫번째 자식) : " + item1.getTagName() + "\n");

                          

                           Node text1 = item1.getFirstChild();

                          textView1.append("text1 엘리먼트 : " + text1.getNodeValue() + "\n==============================\n");

                          

                           //order 마지막 엘리먼트================================

                           Element item3 = (Element)order.getLastChild();                //마지막 자식태그을 가져옴 : item

                           textView1.append("item3(마지막 자식) : " + item3.getTagName() + "\n");

                          

                           Node text3 = item3.getFirstChild();

                          textView1.append("text3 엘리먼트 : " + text3.getNodeValue() + "\n==============================\n");

 

                           //"item1" 이웃 엘리먼트================================

                           Element item2 = (Element)item1.getNextSibling();              //다음것

                           //Element item2 = (Element)item1.getPreviousSibling();                 //이전것

                          

                           textView1.append("item2 : " + item2.getTagName() + "\n");

                          

                           Node text2 = item2.getFirstChild();

                           textView1.append("text2 엘리먼트 : " + text2.getNodeValue() + "\n==============================\n");*/

                          

                           //2번째 방법

                           //getElementsByTagName : 직접 tag 이름으로 가져옴

                           NodeList nodeList = order.getElementsByTagName("item");

                          

                           //getChildNodes : 자식들의 이름이 달라도 전부 가져옴

                           //NodeList nodeList = order.getChildNodes();

                          

                           for(int i = 0 ; i < nodeList.getLength() ; i++){

                                   Node node = nodeList.item(i);      //i번째 엘리먼트를 가져옴

                                   Node text = node.getFirstChild();

                                   textView1.append("item"+ (i+1) +" 엘리먼트 : " + text.getNodeValue() + "\n==============================\n");

                           }

 

                          

                  } catch (UnsupportedEncodingException e) {

                           // TODO Auto-generated catch block

                          

                  } catch (ParserConfigurationException e) {

                           // TODO Auto-generated catch block

                          

                  } catch (SAXException e) {

                           // TODO Auto-generated catch block

                          

                  } catch (IOException e) {

                           // TODO Auto-generated catch block

                          

                  }

         }

 

         @Override

         public boolean onCreateOptionsMenu(Menu menu) {

                  // Inflate the menu; this adds items to the action bar if it is present.

                  getMenuInflater().inflate(R.menu.main, menu);

                  return true;

         }

 

}