XMLSW.scala
23. 10. 2018 #kód
javax.xml.stream.XMLStreamWriter wrapped in a nicer API
object XMLSW { private val f = XMLOutputFactory.newInstance def document(encoding: String = "utf-8", version: String = "1.0")(body: XMLSW => Unit) = { val out = new StringWriter() val w = f.createXMLStreamWriter(out) val ww = new XMLSW(w) ww.document(encoding, version)(body) w.flush() w.close() out.toString } } class XMLSW(val w: XMLStreamWriter) { def document(encoding: String, version: String)(body: XMLSW => Unit) = { w.writeStartDocument(encoding, version) body(this) w.writeEndDocument() } def element(localName: String, content: String) = { w.writeStartElement(localName) w.writeCharacters(content) w.writeEndElement() } def element(localName: String, attributes: XMLSW => Unit, content: String) = { w.writeStartElement(localName) attributes(this) w.writeCharacters(content) w.writeEndElement() } def element(localName: String)(body: XMLSW => Unit) = { w.writeStartElement(localName) body(this) w.writeEndElement() } def attribute(localName: String, value: String) = w.writeAttribute(localName, value) }