private static void writeElement(XMLStreamWriter xtw, Element element) throws XMLStreamException { xtw.writeStartElement(element.getName()); for (String attributeName : element.getAttributeNames()) { xtw.writeAttribute(attributeName, element.getAttributeValue(attributeName)); } if (element.hasText()) { xtw.writeCharacters(element.getText()); } else { for (Element child : element.getChildren()) { writeElement(xtw, child); } } xtw.writeEndElement(); }
// public static void write(OutputStream stream, Element element, int hashLength) throws // XMLStreamException { // XMLOutputFactory xof = XMLOutputFactory.newInstance(); // IndentingXMLStreamWriter xtw = new // IndentingXMLStreamWriter(xof.createXMLStreamWriter(stream)); // writeXMLStreamWriter(xtw, element, hashLength); // } // private static void writeXMLStreamWriter(XMLStreamWriter xtw, Element element, int hashLength) throws XMLStreamException { if (hashLength > 0) { String hash = element.getHash(hashLength); Element hashElement = new Element(CRC_ELEMENT); hashElement.setText(hash); element.addChild(hashElement); } writeElement(xtw, element); xtw.writeEndDocument(); xtw.flush(); xtw.close(); if (hashLength > 0) { element.removeChildren(CRC_ELEMENT); } }
@Override public int writeData(OutputStream out, DbData data) throws Exception { XMLStreamWriter sw = _staxOutFactory.createXMLStreamWriter(out, "UTF-8"); sw.writeStartDocument(); sw.writeStartElement(FIELD_TABLE); Iterator<DbRow> it = data.rows(); while (it.hasNext()) { DbRow row = it.next(); sw.writeStartElement(FIELD_ROW); // <row> sw.writeStartElement(DbRow.Field.id.name()); sw.writeCharacters(String.valueOf(row.getId())); sw.writeEndElement(); sw.writeStartElement(DbRow.Field.firstname.name()); sw.writeCharacters(row.getFirstname()); sw.writeEndElement(); sw.writeStartElement(DbRow.Field.lastname.name()); sw.writeCharacters(row.getLastname()); sw.writeEndElement(); sw.writeStartElement(DbRow.Field.zip.name()); sw.writeCharacters(String.valueOf(row.getZip())); sw.writeEndElement(); sw.writeStartElement(DbRow.Field.street.name()); sw.writeCharacters(row.getStreet()); sw.writeEndElement(); sw.writeStartElement(DbRow.Field.city.name()); sw.writeCharacters(row.getCity()); sw.writeEndElement(); sw.writeStartElement(DbRow.Field.state.name()); sw.writeCharacters(row.getState()); sw.writeEndElement(); sw.writeEndElement(); // </row> } sw.writeEndElement(); sw.writeEndDocument(); sw.close(); return -1; }
/** * Export the placemark attributes to KML as a {@code <Style>} element. The {@code output} object * will receive the data. This object must be one of: java.io.Writer<br> * java.io.OutputStream<br> * javax.xml.stream.XMLStreamWriter * * @param output Object to receive the generated KML. * @throws XMLStreamException If an exception occurs while writing the KML * @see #export(String, Object) */ protected void exportAsKML(Object output) throws XMLStreamException { XMLStreamWriter xmlWriter = null; XMLOutputFactory factory = XMLOutputFactory.newInstance(); boolean closeWriterWhenFinished = true; if (output instanceof XMLStreamWriter) { xmlWriter = (XMLStreamWriter) output; closeWriterWhenFinished = false; } else if (output instanceof Writer) { xmlWriter = factory.createXMLStreamWriter((Writer) output); } else if (output instanceof OutputStream) { xmlWriter = factory.createXMLStreamWriter((OutputStream) output); } if (xmlWriter == null) { String message = Logging.getMessage("Export.UnsupportedOutputObject"); Logging.logger().warning(message); throw new IllegalArgumentException(message); } xmlWriter.writeStartElement("Style"); // Line style xmlWriter.writeStartElement("LineStyle"); final Color lineColor = this.getOutlineMaterial().getDiffuse(); if (lineColor != null) { xmlWriter.writeStartElement("color"); xmlWriter.writeCharacters(KMLExportUtil.stripHexPrefix(WWUtil.encodeColorABGR(lineColor))); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("colorMode"); xmlWriter.writeCharacters("normal"); xmlWriter.writeEndElement(); } final Double lineWidth = this.getOutlineWidth(); if (lineWidth != null) { xmlWriter.writeStartElement("width"); xmlWriter.writeCharacters(Double.toString(lineWidth)); xmlWriter.writeEndElement(); } xmlWriter.writeEndElement(); // LineStyle // Poly style xmlWriter.writeStartElement("PolyStyle"); final Color fillColor = this.getInteriorMaterial().getDiffuse(); if (fillColor != null) { xmlWriter.writeStartElement("color"); xmlWriter.writeCharacters(KMLExportUtil.stripHexPrefix(WWUtil.encodeColorABGR(fillColor))); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("colorMode"); xmlWriter.writeCharacters("normal"); xmlWriter.writeEndElement(); } xmlWriter.writeStartElement("fill"); xmlWriter.writeCharacters(kmlBoolean(isDrawInterior())); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("outline"); xmlWriter.writeCharacters(kmlBoolean(isDrawOutline())); xmlWriter.writeEndElement(); xmlWriter.writeEndElement(); // PolyStyle xmlWriter.writeEndElement(); // Style xmlWriter.flush(); if (closeWriterWhenFinished) xmlWriter.close(); }