Android Sax解析XML格式数据

    在Android开发中,经常会遇到对数据进行解析并获得有用信息的操作。下面使用Sax对XML格式数据进行解析,主要代码如下所示:

		try {

			// 用于测试的URL字符串
			String xmlString = "<NewDataSet><Table><Country>China</Country><City>Beijing</City><ID><Int>1</Int><Float>1.0</Float></ID></Table><Table><Country>China</Country><City>Hohhot</City><ID><Int>2</Int><Float>2.0</Float></ID></Table><Table><Country>China</Country><City>Dalian</City><ID><Int>3</Int><Float>3.0</Float></ID></Table></NewDataSet>";

			// 获得根元素
			RootElement newDataSet = new RootElement("NewDataSet");
			
			// 获得根元素的子元素Table
			Element table = newDataSet.getChild("Table");
			// 添加元素Table开始监听器
			table.setStartElementListener(new StartElementListener() {
				
				@Override
				public void start(Attributes attributes) {
					System.out.println("www:**********************************");
				}
			});
			// 添加元素Table结束监听器
			table.setEndElementListener(new EndElementListener() {

				@Override
				public void end() {
					System.out.println("www:**********************************");
				}
			});
			
			// 获得元素Table的子元素City
			Element city = table.getChild("City");
			// 添加元素City文本结束监听器
			city.setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							System.out.println("www:City: " + body);
						}
					});
			
			// 获得元素Table的子元素Country
			Element country = table.getChild("Country");
			country.setEndTextElementListener(new EndTextElementListener() {
				
				@Override
				public void end(String body) {
					System.out.println("www:Country: " + body);
				}
			});
			
			// 获得元素Table的子元素ID
			Element id = table.getChild("ID");
			
			// 获得元素ID的子元素Int
			Element idInt = id.getChild("Int");
			idInt.setEndTextElementListener(new EndTextElementListener() {
				
				@Override
				public void end(String body) {
					System.out.println("www:Int: " + body);
				}
			});
			
			// 获得元素ID的子元素Float
			Element idFloat = id.getChild("Float");
			idFloat.setEndTextElementListener(new EndTextElementListener() {
				
				@Override
				public void end(String body) {
					System.out.println("www:Float: " + body);
				}
			});

			// 获得SAXParserFactory实例
			SAXParserFactory factory = SAXParserFactory.newInstance();
			
			// 获得SAXParser实例
			SAXParser parser = factory.newSAXParser();

			// 获得XMLReader实例
			XMLReader xmlreader = parser.getXMLReader();
			xmlreader.setContentHandler(newDataSet.getContentHandler());
			
			InputSource is = new InputSource(new StringBufferInputStream(xmlString));

			// 解析
			xmlreader.parse(is);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

     其中需要解析的XML格式数据如下:

<NewDataSet>
	<Table>
		<Country>China</Country>
		<City>Beijing</City>
		<ID>
			<Int>1</Int>
			<Float>1.0</Float>
		</ID>
	</Table>
	<Table>
		<Country>China</Country>
		<City>Hohhot</City>
		<ID>
			<Int>2</Int>
			<Float>2.0</Float>
		</ID>
	</Table>
	<Table>
		<Country>China</Country>
		<City>Dalian</City>
		<ID>
			<Int>3</Int>
			<Float>3.0</Float>
		</ID>
	</Table>
</NewDataSet>

     输出效果如下:

www:**********************************
www:Country: China
www:City Name: Beijing
www:Int: 1
www:Float: 1.0
www:**********************************
www:**********************************
www:Country: China
www:City Name: Hohhot
www:Int: 2
www:Float: 2.0
www:**********************************
www:**********************************
www:Country: China
www:City Name: Dalian
www:Int: 3
www:Float: 3.0
www:**********************************

     流程比较简单,就不详细说明了!希望对您有所帮助!:)

 

你可能感兴趣的:(SAXParser,Sax解析,解析XML格式数据)