YesYo.com MintState Forums
뒤로    YesYo.com MintState BBS > Tech > Android
검색
멤버이름    오토
비밀번호 
 

Android XML Parsing Tutorial – Using DOMParser

페이지 정보

작성자 MintState 댓글 0건 조회 5,068회 작성일 11-06-07 22:23

본문

Android XML Parsing Tutorial – Using DOMParser

XML 파싱예제입니다. DOM파서를 이용한 방법입니다.



XML
<?xml version="1.0"?>
<maintag>
   <item>
	<name>AndroidPeople</name>
	<website category="android">www.androidpeople.com</website>
	</item>
	<item>
	<name>iPhoneAppDeveloper</name>
	<website category="iPhone">www.iphone-app-developer.com</website>
	</item>
</maintag>


Main Java
public class XMLParsingDOMExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
		/** Create a new layout to display the view */
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(1);

		/** Create a new textview array to display the results */
		TextView name[];
		TextView website[];
		TextView category[];

		try {

			URL url = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new InputSource(url.openStream()));
			doc.getDocumentElement().normalize();

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

			/** Assign textview array lenght by arraylist size */
			name = new TextView[nodeList.getLength()];
			website = new TextView[nodeList.getLength()];
			category = new TextView[nodeList.getLength()];

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

				Node node = nodeList.item(i);

				name[i] = new TextView(this);
				website[i] = new TextView(this);
				category[i] = new TextView(this);

				Element fstElmnt = (Element) node;
				NodeList nameList = fstElmnt.getElementsByTagName("name");
				Element nameElement = (Element) nameList.item(0);
				nameList = nameElement.getChildNodes();
				name[i].setText("Name = "
						+ ((Node) nameList.item(0)).getNodeValue());

				NodeList websiteList = fstElmnt.getElementsByTagName("website");
				Element websiteElement = (Element) websiteList.item(0);
				websiteList = websiteElement.getChildNodes();
				website[i].setText("Website = "
						+ ((Node) websiteList.item(0)).getNodeValue());

				category[i].setText("Website Category = "
						+ websiteElement.getAttribute("category"));

				layout.addView(name[i]);
				layout.addView(website[i]);
				layout.addView(category[i]);

			}
		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}

		/** Set the layout view to display */
		setContentView(layout);
    }
}


URL : http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser
첨부 파일
파일 종류: png androidxmlparsing.png (12.3K, 1 views)
파일 종류: zip XMLParsing-Using-DOM.zip (45.1K, 3 views)

댓글목록

등록된 댓글이 없습니다.

Total 18건 1 페이지
게시물 검색
모바일 버전으로 보기
CopyRight ©2004 - 2018, YesYo.com MintState. ™