Android Sax解析XML格式数据

本文介绍了如何在Android开发中使用SAX解析XML格式数据,包括解析流程、关键代码及输出效果,帮助开发者理解XML数据解析的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在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:**********************************

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值