前端之家收集整理的这篇文章主要介绍了
SAX 解析XML 文件,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用 SAX 解析XML 文件,不说那么多,贴代码:
解析xml:
package com.example.xmldemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* 任务图形xml解析
* @author cyj
*
*/
public class XMLPaserHandle extends DefaultHandler {
final int MISSION = 1;
final int ESRIGEOMETRYPOINT = 2;//点
final int RECORD = 3;
final int FID = 4;
final int NAME = 5;
final int GUID = 6;
final int POINTS = 7;
final int ESRIGEOMETRYPOLYLINE = 8;//线
final int ESRIGEOMETRYPOLYGON = 9;//面
int currentstate = 0;
int recordState = 0;
private Map<String,List<MissionRecordPO>> missions;
/**点**/
private List<MissionRecordPO> pointMissionRecordPO;
/**线**/
private List<MissionRecordPO> polylineMissionRecordPO;
/**面**/
private List<MissionRecordPO> polygonMissionRecordPO;
/**记录**/
private MissionRecordPO missionRecordPO;
/**<mission guid="dec20ec7-e019-41c1-9ec0-9b57792eb27b"> 对应guid**/
private String missionGuid = null;
public XMLPaserHandle(){}
public Map<String,List<MissionRecordPO>> getMissions(){
return this.missions;
}
@Override
public void startDocument() throws SAXException {
missions = new HashMap<String,List<MissionRecordPO>>();
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {
//String missionGuid = attributes.getValue("guid");
if(localName.equalsIgnoreCase("mission")){
missionGuid = attributes.getValue("guid");
currentstate = 0;
return;
}
if(localName.equalsIgnoreCase(MissionRecordPO.IMPORT_XML_NODE_NAME_POINT)){
pointMissionRecordPO = new ArrayList<MissionRecordPO>();
currentstate = ESRIGEOMETRYPOINT;
return;
}
if(localName.equalsIgnoreCase(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYLINE)){
polylineMissionRecordPO = new ArrayList<MissionRecordPO>();
currentstate = ESRIGEOMETRYPOLYLINE;
return;
}
if(localName.equalsIgnoreCase(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYGON)){
polygonMissionRecordPO = new ArrayList<MissionRecordPO>();
currentstate = ESRIGEOMETRYPOLYGON;
return;
}
if(localName.equalsIgnoreCase("record")){
missionRecordPO = new MissionRecordPO();
missionRecordPO.setMissionGuid(missionGuid);
//currentstate = RECORD;
return;
}
if(localName.equalsIgnoreCase("fid")){
recordState = FID;
return;
}
if(localName.equalsIgnoreCase("name")){
recordState = NAME;
return;
}
if(localName.equalsIgnoreCase("guid")){
recordState = GUID;
return;
}
if(localName.equalsIgnoreCase("points")){
recordState = POINTS;
return;
}
recordState = 0;
currentstate = 0;
}
@Override
public void endElement(String uri,String qName)
throws SAXException {
// TODO Auto-generated method stub
if(localName.equalsIgnoreCase("mission")){
return;
}
if(localName.equalsIgnoreCase(MissionRecordPO.IMPORT_XML_NODE_NAME_POINT)){
missions.put(MissionRecordPO.IMPORT_XML_NODE_NAME_POINT,pointMissionRecordPO);
return;
}
if(localName.equalsIgnoreCase(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYLINE)){
missions.put(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYLINE,polylineMissionRecordPO);
return;
}
if(localName.equalsIgnoreCase(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYGON)){
missions.put(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYGON,polygonMissionRecordPO);
return;
}
if(localName.equalsIgnoreCase("record")){
if(ESRIGEOMETRYPOINT == currentstate){
pointMissionRecordPO.add(missionRecordPO);
}else if(ESRIGEOMETRYPOLYLINE == currentstate){
polylineMissionRecordPO.add(missionRecordPO);
}else if(ESRIGEOMETRYPOLYGON == currentstate){
polygonMissionRecordPO.add(missionRecordPO);
}
return;
}
}
@Override
public void characters(char[] ch,int start,int length)
throws SAXException {
// TODO Auto-generated method stub
String theString = new String(ch,start,length);
theString = theString.trim();
if(null == theString || "".equals(theString) || "\n".equals(theString)){
//currentstate = 0;
return;
}
switch(recordState){
case FID:
missionRecordPO.setFid(theString);
// currentstate = 0;
break;
case NAME:
missionRecordPO.setName(theString);
//currentstate = 0;
break;
case GUID:
missionRecordPO.setGuid(theString);
//currentstate = 0;
break;
case POINTS:
missionRecordPO.setPoints(theString);
// currentstate = 0;
break;
default:
return;
}
}
}
Activity显示解析内容:
package com.example.xmldemo;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ContentValues;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//final String path ="file://" + Environment.getExternalStorageDirectory() + "/xmldemo/任务图形.xml";
//Log.i("path",path);
Button btnPare = (Button) findViewById(R.id.btn_parse);
btnPare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.pase);
ll.setVisibility(View.VISIBLE);
dealImportData(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
public void dealImportData(String path) {
Map<String,List<MissionRecordPO>> missions = xmlTaskGraphicHandle(path);
List<MissionRecordPO> importPoints = missions
.get(MissionRecordPO.IMPORT_XML_NODE_NAME_POINT);
List<MissionRecordPO> importPolylines = missions
.get(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYLINE);
List<MissionRecordPO> importPolygons = missions
.get(MissionRecordPO.IMPORT_XML_NODE_NAME_POLYGON);
if (null != importPoints) {// 点
TextView tvPoint = (TextView) findViewById(R.id.point);
tvPoint.setText(importPoints.get(0).getPoints());//第一条记录,下同
}
if (null != importPolylines) {// 线
TextView tvLine = (TextView) findViewById(R.id.polyline);
tvLine.setText(importPolylines.get(0).getPoints());
}
if (null != importPolygons) {// 面
TextView tvGon = (TextView) findViewById(R.id.polygon);
tvGon.setText(importPolygons.get(0).getPoints());
}
}
private Map<String,List<MissionRecordPO>> xmlTaskGraphicHandle(String path) {
Map<String,List<MissionRecordPO>> missions = null;
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
XMLPaserHandle handler = new XMLPaserHandle();
reader.setContentHandler(handler);
InputSource is = new InputSource(this.getClassLoader().getResourceAsStream("任务图形.xml"));
reader.parse(is);
missions = handler.getMissions();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return missions;
}
}