更新kml标签

时间:2013-10-02 16:30:02

标签: xml regex google-maps kml qgis

我正在研究google-maps kml。我有几个shapefile并使用QGIS我已将其转换为kml文件。我需要更新标签结构以应用BalloonStyle。kml add extended data 我需要转换标签结构,如

            <SchemaData schemaUrl="#new_circle">
                <SimpleData name="Name">7</SimpleData>
                <SimpleData name="gid">1</SimpleData>
                <SimpleData name="active_sub">4596</SimpleData>
                <SimpleData name="net_additi">5181</SimpleData>
                <SimpleData name="gross_addi">3917</SimpleData>
                <SimpleData name="churn">4617</SimpleData>
            </SchemaData>-->

转换为

<Data name="name">
                    <value>7</value>
                </Data>
                <Data name="active_sub">
                    <value>4596</value>
                </Data>
                <Data name="net_additi">
                    <value>5181</value>
                </Data>
                <Data name="gross_additi">
                    <value>3917</value>
                </Data>
                <Data name="churn">
                    <value>4617</value>
                </Data>

是否有任何查找和替换算法或任何其他软件导出shp到kml我检查了不同的软件和在线转换器,也检查ogr2ogr但所有这些给标签结构与简单的数据。
提前谢谢....

1 个答案:

答案 0 :(得分:0)

我通过使用jdom库创建java类来更新kml。因为我需要做一些手动任务。删除kml标记,然后处理文件。这是我为此创建的课程

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
 * @author amit
 */
public class XMLParser {
    private void getXMLDetails() {
        try {
            SAXBuilder builder = new SAXBuilder();
            File file = new File("sample.xml");
            if (file.exists()) {
                Document document = (Document) builder.build(file);
                Element root = document.getRootElement();
                System.out.println(root.getChildren());
                Element firstTag = root.getChild("Folder");
                //firstTag.getAttribute("tag").setValue("file");
                List rootdata = firstTag.getChildren();
                System.out.println(rootdata);
                for(Iterator<Element> i = rootdata.iterator(); i.hasNext(); ) {
                    Element rootitem =i.next();
                    if(rootitem.getName() =="Placemark"){
                    System.out.println(rootitem.getChildren("ExtendedData"));
                    List listdata = rootitem.getChild("ExtendedData").getChild("SchemaData").getChildren();
                    for(Iterator<Element> j = listdata.iterator(); j.hasNext(); ) {
                        Element item =j.next();
                        System.out.println(item);
                        String gidval = item.getText();
                        Element gid = new Element("value").setText(gidval);
                        item.setText("");
                        item.addContent(gid);
                        System.out.println("test");
                    }
                    }
                }
                String des = new XMLOutputter().outputString(document);
                des = des.replaceAll("SimpleData", "Data");
                des = des.replaceAll("</SchemaData>", "");
                des = des.replaceAll("<SchemaData schemaUrl=\"#new_area\">", "");
                System.out.println("String: " + des.replaceAll("SimpleData", "Data"));
                FileWriter fileWriter = new FileWriter(file);
                fileWriter.write(des);
                fileWriter.close();
            } else {
                System.out.println("File does not exist");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String argS[]) {
        try {
            new XMLParser().getXMLDetails();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

以下是jar JDOM lib的链接
感谢