java kml_Java(Android)解析KML文件

package com.test.parsekml;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Iterator;

import java.util.zip.ZipEntry;

import java.util.zip.ZipException;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.jsoup.Jsoup;

import org.jsoup.select.Elements;

import android.util.Log;

public class ReadKml {

public void parseKml(String pathName) throws Exception

{

File file = new File(pathName);//pathName为KML文件的路径

try {

ZipFile zipFile = new ZipFile(file);

ZipInputStream zipInputStream = null;

InputStream inputStream = null;

ZipEntry entry = null;

zipInputStream = new ZipInputStream(new FileInputStream(file));

while ((entry = zipInputStream.getNextEntry()) != null) {

String zipEntryName = entry.getName();

Log.d("压缩实体的名称:", zipEntryName);

if (zipEntryName.endsWith("kml") || zipEntryName.endsWith("kmz")) {

inputStream = zipFile.getInputStream(entry);

parseXmlWithDom4j(inputStream);

}else if (zipEntryName.endsWith("png")) {

/*ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

byte[] b = new byte[512];

int readedByteSize = 0;

while ((readedByteSize = zipInputStream.read(b)) != -1) {

byteArrayOut.write(b, 0, readedByteSize);

}

byteArrayOut.flush();

byteArrayOut.close();

InputStream isBitmap = new ByteArrayInputStream(byteArrayOut.toByteArray());

Bitmap bitmap = BitmapFactory.decodeStream(isBitmap);

isBitmap.close();*/

}

}

zipInputStream.close();

inputStream.close();

} catch (ZipException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void parseXmlWithDom4j(InputStream input) throws Exception

{

SAXReader reader = new SAXReader();

Document document = null;

try {

document = reader.read(input);

} catch (DocumentException e) {

// TODO: handle exception

e.printStackTrace();

}

Element root = document.getRootElement();//获取doc.kml文件的根结点

listNodes(root);

}

//遍历当前节点下的全部节点

public void listNodes(Element node){

Log.d("当前结点的名称:", node.getName());

//首先获取当前节点的全部属性节点

/* List list = node.attributes();

//遍历属性节点

for(Attribute attribute : list){

Log.d("属性", attribute.getName() +":" + attribute.getValue());

} */

//假设当前节点内容不为空,则输出

if(!(node.getTextTrim().equals("")) && "description".equals(node.getName())){

//Log.d("当前结点内容:", node.getText());

parseHtml(node.getText());

}

//同一时候迭代当前节点以下的全部子节点

//使用递归

Iterator iterator = node.elementIterator();

while(iterator.hasNext()){

Element e = iterator.next();

listNodes(e);

}

}

public void parseHtml(String htmlData)

{

org.jsoup.nodes.Document document = Jsoup.parse(htmlData);

Elements trs = document.select("table").select("tr");

String trContent = "";

String trContentSplit[] = null;

String x = "";

String y = "";

String name = "";

for (int i = 2; i < trs.size(); i++) {//在KML文件里的HTML文本中,共同拥有13个tr,每一个tr包括了一个属性,当中第二个tr包括了全部的属性,因此我们在处理时从第三个tr開始

trContent = trs.get(i).text();

trContentSplit = trContent.split(" ");

if ("name".equals(trContentSplit[0])) {

name = trContentSplit[1];

}

if ("x".equals(trContentSplit[0]) || "X".equals(trContentSplit[0])) {

x = trContentSplit[1].trim();

}

if ("y".equals(trContentSplit[0]) || "Y".equals(trContentSplit[0])) {

y = trContentSplit[1].trim();

}

/*Elements tds = elements.get(i).select("td");

for (int j = 0; j < tds.size(); j++) {

htmlContent = tds.get(j).text();

}*/

}

Log.d("X:", x);

Log.d("Y:", y);

Log.d("Name:", name);

}

}

Android解析KML文件的步骤如下: 1. 创建SAXParserFactory实例。 2. 创建SAXParser实例。 3. 创建DefaultHandler的子类实例,覆盖startElement()、characters()、endElement()等方法。 4. 调用SAXParser的parse()方法,传入KML文件的InputStream和DefaultHandler实例,开始解析。 5. 在startElement()方法中,可以获取到每个标签的名称和属性值。 6. 在characters()方法中,可以获取到每个标签中的文本内容。 7. 在endElement()方法中,可以处理当前标签的子标签和属性值。 以下是一个简单的例子,演示如何解析KML文件: ```java public class KmlParser { public static void parse(InputStream inputStream) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean inCoordinates = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("coordinates")) { inCoordinates = true; } } public void characters(char[] ch, int start, int length) throws SAXException { if (inCoordinates) { String coords = new String(ch, start, length); // 坐标处理逻辑 inCoordinates = false; } } }; parser.parse(inputStream, handler); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的例子中,我们只处理了KML文件中的coordinates标签,读取其中的坐标信息。如果需要处理其他标签或属性,可以在DefaultHandler子类中覆盖相应的方法进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值