packagegao.map.preprocess;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStreamWriter;importjavax.xml.parsers.ParserConfigurationException;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importorg.xml.sax.helpers.DefaultHandler;importorg.xml.sax.Attributes;importorg.xml.sax.SAXException;/*** 处理从OpenStreetMap下载的原始数据,将抽取的数据输出为text文件
*@authorAdministrator
**/
public classOpenStreetMap {static String s = null;//实现自己的解析方式
static class MySAXHandler extendsDefaultHandler {
FileOutputStream fosPoint;
FileOutputStream fosArc;
OutputStreamWriter oswPoint;
OutputStreamWriter oswArc;
BufferedWriter bwPoint;
BufferedWriter bwArc;//开始解析时调用
public void startDocument() throwsSAXException {//点信息
File pointFile = new File("D:\\项目\\mongo\\MapPre\\Point.txt");//弧信息
File arcFile = new File("D:\\项目\\mongo\\MapPre\\Arc.txt");try{
fosPoint= newFileOutputStream(pointFile);
fosArc= newFileOutputStream(arcFile);
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
oswPoint= newOutputStreamWriter(fosPoint);
oswArc= newOutputStreamWriter(fosArc);
bwPoint= newBufferedWriter(oswPoint);
bwArc= newBufferedWriter(oswArc);
System.out.println("开始解析文档!");
}//完成解析时调用
public void endDocument() throwsSAXException {try{
bwPoint.close();
oswPoint.close();
fosPoint.close();
bwArc.close();
oswArc.close();
fosArc.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文档解析完成!");
}/*** 开始一个元素的解析
*@paramuri
*@paramlocalName
*@paramqName 标签名
*@paramattributes 属性
*@throwsSAXException*/
public voidstartElement(String uri, String localName, String qName,
Attributes attributes)throwsSAXException {if ((attributes != null)&&attributes.getLength() > 0) {if(qName.equals("node")){
StringBuilder sb= newStringBuilder();
sb.append(attributes.getValue("id")+" ");
sb.append(attributes.getValue("lat")+" ");
sb.append(attributes.getValue("lon"));try{
bwPoint.write(sb.toString()+"\r\n");
bwPoint.flush();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sb.toString());
}else if(qName.equals("way")){
s= attributes.getValue("id")+" "
+attributes.getValue("version")+" ";
}else if(qName.equals("nd")){if(s==null)return;try{
bwArc.write(s+attributes.getValue("ref")+" \r\n");
bwArc.flush();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(s+attributes.getValue("ref")+" ");
}else if(qName.equals("tag")){if(s==null)return;try{
bwArc.write(s+" "+attributes.getValue("k")+"\r\n");
bwArc.flush();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(s+" "+attributes.getValue("k"));
}else if(qName.equals("relation")){if(s!=null)
s= null;
}
}
}/*** 结束一个元素的解析,遇到结束标签时调用此方法 通常在此方法对标签取值并处理
*@paramuri
*@paramlocalName
*@paramqName
*@throwsSAXException*/
public voidendElement(String uri, String localName, String qName)throwsSAXException {
}//该方法是获得元素间的text文本内容,可以通过new String(ch, start, length)来获得
public void characters(char[] ch, int start, intlength)throwsSAXException {//System.out.print(new String(ch, start, length));
}
}public static voidmain(String[] args) {
SAXParserFactory saxfac=SAXParserFactory.newInstance();try{
SAXParser saxparser=saxfac.newSAXParser();
InputStream is= new FileInputStream("D:\\项目\\map");
MySAXHandler handler= newMySAXHandler();
saxparser.parse(is, handler);;
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(ParserConfigurationException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(SAXException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}