SAX解析学习总结

来源:互联网 发布:佳能mg3580清零软件 编辑:程序博客网 时间:2024/05/01 06:11

由于DOM解析时要一次把所有的内容全读出来,所以其不适合进行大数据的操作

SAX解析

1.首先要定义一个SAX 解析器,及一个存储xml信息的类。

1)定义SAX解析器(记为MySAX)

2)重写startDocument(),startElement(),characters(),endElements()方法

解析器的主要功能是将指定xml文档中的数据取出并将数据封装成自己定义的类的对象的集合,最后将所有数据返回.


SAX解析

1.指定要解析的文件路径。(记为file)

2.建立SAX解析工厂

SAXParserFactory  factory = SAXParserFactory.newInstance();

3.构造解析器

SAXParser parser = null;

MySax sax = new MySax();

parser = factory.newSAXParser();

4.解析xml使用DefaultHandler

parser.parser(file,sax);

5.定义集合接收解析内容。


定义SAX解析器(记为MySAX)

public class MySax extends DefaultHandler {
private List<LinkMan> all = null;
private String elementname = null;
private LinkMan man = null;

重写方法
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
this.all = new ArrayList<LinkMan>();
}


@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if("linkman".equals(localName))
{
this.man = new LinkMan();
}
this.elementname = localName;
}


@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("linkman".equals(localName))
{
this.all.add(this.man);
this.man = null;
}
this.elementname = null;

}


@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(this.elementname != null)
{
String data = new String(ch,start,length);//取的文字信息
if("name".equals(this.elementname))
{
this.man.setName(data);
}
else if("email".equals(this.elementname))
{
this.man.setEmail(data);
}

}

}


public List<LinkMan> getAll() {
return this.all;
}




}






public class MainActivity extends Activity {
private TextView name = null;
private TextView email = null;
private Button but = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.but = (Button)findViewById(R.id.but);
this.email = (TextView)findViewById(R.id.name);
this.name = (TextView)findViewById(R.id.email);
this.but.setOnClickListener(new myonclicklisten());
}
private class myonclicklisten implements OnClickListener{


@Override
public void onClick(View v) {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
return;
}
File file = new File(Environment.getExternalStorageDirectory().toString()
+File.separator
+"mldndata"
+File.separator
+"member.xml"
);
if(!file.exists())
{
return;

}
//建立SAX解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();

//构造解析器
SAXParser parser = null;
MySax sax = new MySax();
try {
parser = factory.newSAXParser();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//解析xml使用DefaultHandler
try {
parser.parse(file, sax);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回信息
List<LinkMan> all = sax.getAll();
MainActivity.this.name.setText(all.get(0).getName());
MainActivity.this.email.setText(all.get(0).getEmail());
}




}
}

0 0
原创粉丝点击