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;
}
}
'JAVA > Android' 카테고리의 다른 글
04-09 쉅내용 (0) | 2013.04.09 |
---|---|
네트워크(Network) : SAX 파서 (0) | 2013.04.09 |
네트워크(Network) : xml [xml, json] (0) | 2013.04.09 |
Spinner에서 선택한 항목 가져오기 (0) | 2013.04.07 |
레이아웃(Layout) : 레이아웃 파라미터 변경 (LayoutParams) (0) | 2013.03.27 |