Android XML Parsing Tutorial – Using DOMParser
페이지 정보
작성자 MintState 댓글 0건 조회 10,768회 작성일 11-06-07 22:23본문
Android XML Parsing Tutorial – Using DOMParser
XML 파싱예제입니다. DOM파서를 이용한 방법입니다.
XML
Main Java
URL : http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-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
|
댓글목록
등록된 댓글이 없습니다.