w.startElement("layer");
if(l.getClass() == SelectionLayer.class) { w.startElement("selection"); } else { w.startElement("layer"); }
    private void writeMapLayer(MapLayer l, XMLWriter w) throws IOException {        try {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            OutputStream out;            boolean encodeLayerData = TiledConfiguration.keyHasValue(                    "tmx.save.encodeLayerData", "1");            boolean compressLayerData = TiledConfiguration.keyHasValue(                    "tmx.save.layerCompression", "1") && encodeLayerData;            Rectangle bounds = l.getBounds();            w.startElement("layer");            //w.writeAttribute("id", "" + l.getId());            w.writeAttribute("name", l.getName());            if (bounds.x != 0) {                w.writeAttribute("xoffset", "" + bounds.x);            }            if (bounds.y != 0) {                w.writeAttribute("yoffset", "" + bounds.y);            }            if (!l.isVisible()) {                w.writeAttribute("visible", "0");            }            if (l.getOpacity() < 1.0f) {                w.writeAttribute("opacity", "" + l.getOpacity());            }                        if (encodeLayerData) {                w.startElement("data");                w.writeAttribute("encoding", "base64");                if (compressLayerData) {                    w.writeAttribute("compression", "gzip");                    out = new GZIPOutputStream(baos);                } else {                    out = baos;                }                for (int y = 0; y < l.getHeight(); y++) {                    for (int x = 0; x < l.getWidth(); x++) {                        Tile tile = l.getTileAt(x, y);                        int gid = 0;                        if (tile != null) {                            gid = tile.getGid();                        }                        out.write((gid      ) & 0x000000FF);                        out.write((gid >>  8) & 0x000000FF);                        out.write((gid >> 16) & 0x000000FF);                        out.write((gid >> 24) & 0x000000FF);                    }                }                if (compressLayerData) {                    ((GZIPOutputStream)out).finish();                }                w.writeCDATA(new String(Base64.encode(baos.toByteArray())));                w.endElement();            } else {                w.startElement("data");                for (int y = 0; y < l.getHeight(); y++) {                    for (int x = 0; x < l.getWidth(); x++) {                        Tile tile = l.getTileAt(x, y);                        int gid = 0;                        if (tile != null) {                            gid = tile.getGid();                        }                        w.startElement("tile");                        w.writeAttribute("gid", ""+gid);                        w.endElement();                    }                }                w.endElement();            }            w.endElement();        } catch (XMLWriterException e) {            e.printStackTrace();        }    }