SimpleXML in Java add CDATA String to KMZ

68 Views Asked by At

I have a program that gets a csv and conver it into a kmz that can be opened in Google Earth

The problem is that one of the inputs is the Description that has a table inside

So I have this:

@Element(name = "Description")
private String description;

And I create the html structure and add the element

String htmlTable = "<table border=&apos;1&apos; width=&apos;300&apos;><tr><td>" + 
contenidoProcesado[1] + "</td></tr></table>";
String description = "<![CDATA[<div>" + htmlTable + "</div>]]>";
placemark.setDescription(description);

Later I use a Serializer with a list of all my entries in the csv as placemarks and create this kmz with a kml inside with all the data.

try {
        Serializer serializer = new Persister();            
        Document kml = new Document(placemarks);                                
                
        try {
            // Read in the KML file
            File kmlFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".kml" );
            String kmlFileName = kmlFile.getName();               
            FileInputStream fis = new FileInputStream(kmlFile);
            byte[] kmlBytes = new byte[(int) kmlFile.length()];
            fis.read(kmlBytes);
            fis.close();                

            // Create the KMZ file
            File kmzFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".kmz" );
            FileOutputStream fos = new FileOutputStream(kmzFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            // Add the KML file to the KMZ archive
            ZipEntry kmlEntry = new ZipEntry(kmlFileName);
            zos.putNextEntry(kmlEntry);
            zos.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n").getBytes());
            zos.write(("<kml xmlns=\"http://www.opengis.net/kml/2.2\">" + "\n").getBytes());
            serializer.write(kml, zos); // write KML data directly to the KMZ file
            zos.write("</kml>".getBytes()); // add closing </kml> tag to the KMZ file
                                            
            zos.closeEntry();

I can Open the mz generated in Google Earth but the description is not appearing and if I open the kml it's like this:

<Description>&lt;div&gt;&lt;table border=&amp;apos;1&amp;apos; width=&amp;apos;300&amp;apos;&gt;&lt;tr&gt;&lt;td&gt;LAM450520&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;</Description>

It does not have an html structure because it converted the "", < and > into special quotes so Google Earth cannot display this description.

Could you guide me a little to finally have the structure unaltered, so I can read it from Google Earth please?

0

There are 0 best solutions below