android sax html 解析xml,如何在android中使用sax解析器从xml读取imageU...

检查以下URl以了解有关XML解析器的信息

首先从URL获取数据.存储在文件中.使用以下代码使用SAXParser解析XML

SAX解析器解析XML

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserExample extends DefaultHandler{

List myEmpls;

private String tempVal;

//to maintain context

private Employee tempEmp;

public SAXParserExample(){

myEmpls = new ArrayList();

}

public void runExample() {

parseDocument();

printData();

}

private void parseDocument() {

//get a factory

SAXParserFactory spf = SAXParserFactory.newInstance();

try {

//get a new instance of parser

SAXParser sp = spf.newSAXParser();

//parse the file and also register this class for call backs

sp.parse("employees.xml", this);

}catch(SAXException se) {

se.printStackTrace();

}catch(ParserConfigurationException pce) {

pce.printStackTrace();

}catch (IOException ie) {

ie.printStackTrace();

}

}

/**

* Iterate through the list and print

* the contents

*/

private void printData(){

System.out.println("No of Employees '" + myEmpls.size() + "'.");

Iterator it = myEmpls.iterator();

while(it.hasNext()) {

System.out.println(it.next().toString());

}

}

//Event Handlers

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

//reset

tempVal = "";

if(qName.equalsIgnoreCase("Employee")) {

//create a new instance of employee

tempEmp = new Employee();

tempEmp.setType(attributes.getValue("type"));

}

}

public void characters(char[] ch, int start, int length) throws SAXException {

tempVal = new String(ch,start,length);

}

public void endElement(String uri, String localName, String qName) throws SAXException {

if(qName.equalsIgnoreCase("Employee")) {

//add it to the list

myEmpls.add(tempEmp);

}else if (qName.equalsIgnoreCase("Name")) {

tempEmp.setName(tempVal);

}else if (qName.equalsIgnoreCase("Id")) {

tempEmp.setId(Integer.parseInt(tempVal));

}else if (qName.equalsIgnoreCase("Age")) {

tempEmp.setAge(Integer.parseInt(tempVal));

}

}

public static void main(String[] args){

SAXParserExample spe = new SAXParserExample();

spe.runExample();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值