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

Android XML Parsing Tutorial - Using SAXParser

페이지 정보

작성자 MintState 댓글 0건 조회 10,795회 작성일 11-06-07 22:30

본문

Android XML Parsing Tutorial - Using SAXParser

SAX 파서를 이용한 XML 파싱 예제입니다.



첨부파일 설명
XMLParsingExample.java ( Main Activity )
SitesList.java ( Getter & Setter Method )
MyXMLHandler.java ( XML Handling )
example.xml ( XML file from net )

example.xml
<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>


XMLParsingExample.java
public class XMLParsingExample extends Activity {
	/** Create Object For SiteList Class */
	SitesList sitesList = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** 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 {
			
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			/** Send URL to parse XML Tags */
			URL sourceUrl = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

			/** Create handler to handle XML Tags ( extends DefaultHandler ) */
			MyXMLHandler myXMLHandler = new MyXMLHandler();
			xr.setContentHandler(myXMLHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));
			
		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}

		/** Get result from MyXMLHandler SitlesList Object */
		sitesList = MyXMLHandler.sitesList;

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

		/** Set the result text in textview and add it to layout */
		for (int i = 0; i < sitesList.getName().size(); i++) {
			name[i] = new TextView(this);
			name[i].setText("Name = "+sitesList.getName().get(i));
			website[i] = new TextView(this);
			website[i].setText("Website = "+sitesList.getWebsite().get(i));
			category[i] = new TextView(this);
			category[i].setText("Website Category = "+sitesList.getCategory().get(i));

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

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


MyXMLHandler.java
public class MyXMLHandler extends DefaultHandler {
	Boolean currentElement = false;
	String currentValue = null;
	public static SitesList sitesList = null;

	public static SitesList getSitesList() {
		return sitesList;
	}

	public static void setSitesList(SitesList sitesList) {
		MyXMLHandler.sitesList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("maintag"))
		{
			/** Start */ 
			sitesList = new SitesList();
		} else if (localName.equals("website")) {
			/** Get attribute value */
			String attr = attributes.getValue("category");
			sitesList.setCategory(attr);
		}
	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */ 
		if (localName.equalsIgnoreCase("name"))
			sitesList.setName(currentValue);
		else if (localName.equalsIgnoreCase("website"))
			sitesList.setWebsite(currentValue);
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
	}
}


SitesList.java
public class SitesList {
	/** Variables */
	private ArrayList<String> name = new ArrayList<String>();
	private ArrayList<String> website = new ArrayList<String>();
	private ArrayList<String> category = new ArrayList<String>();

	
	/** In Setter method default it will return arraylist 
	 *  change that to add  */
	
	public ArrayList<String> getName() {
		return name;
	}

	public void setName(String name) {
		this.name.add(name);
	}

	public ArrayList<String> getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website.add(website);
	}

	public ArrayList<String> getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category.add(category);
	}
}


URL : http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser
첨부 파일
파일 종류: png androidxmlparsing.png (12.3K, 40 views)
파일 종류: zip XMLParsing.zip (49.1K, 9 views)

댓글목록

등록된 댓글이 없습니다.

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