1

I am working on google-maps kml. I have several shapefiles and using QGIS i have converted it to kml files. When i export the kml (save as kml in Qgis). It contains tag like
<SimpleData name="Name">7</SimpleData> but i want tag structure like
<Data name="name"> <value>7</value> </Data> I have export the shape as kml and update the kml change style add BalloonStyle for that i need to modify structure otherwise it will not display info in popup window.Here is the link for rules of tag structure to display popup in google map as per kml extended data

If we use <SimpleData> tag then popup will not display lables in google map

wrong popup
When i change the structure of tags to <Data><Value>it will works gr8 ,Expected popup window is expected popup
I just want to change the tag structure.

Is there an automated way to change the tag structure from kml file?

For example, like below <simppledata> to <data><value>

<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>` 

to

<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>

here is the sample link in which i have commented the simpledata tag and add manually data and value tag.expected kml output To do it manually will take maximum time because i have thousands of records in other layers.

0
2

I have updated the kml by creating java class using jdom library.For that i need to do some manual task. Remove kml tag and then process file. Here is the class i have created for this

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();
        }
    }
}

Here is the link for jarJDOM library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.