Is it possible to specify the behavior of EOL when using XMLOutputFactory?

82 Views Asked by At

I like to write an xml Elem to a file, but I want to set the line separator between each node of the elem, so when opening the file under windows or ubuntu I have the correct format. for example :

val lineSep = System.getProperty("line.separator") 
val xmlData : Elem = <person>
       <firstName>John</firstName>
       <lastName>Doe</lastName>
  <emails>
  <email type=”primary”>[email protected]</email>
  <email type=”secondary”>[email protected]</email>
  </emails>
  <address>
  <street>595 Market Street</street>
  <city>San Francisco</city>
  <zip>94105</zip>
  </address>
  </person>

val xmlStreamWriter = XMLOutputFactory.newInstance.createXMLStreamWriter(outputstream)
xmlStreamWriter.writeDTD(xmlData)

How to write it into a file considering the lineSeparator? the XMLOutputFactory have the method setProperty() is it possible to specify the line separator here?

1

There are 1 best solutions below

2
On

From The standard Scala XML library - Getting started guide:

To write XML to a file:

scala.xml.XML.save("books.xml", books)

To format XML use the scala.xml.PrettyPrinter to configure the line length and indentation level:

val pp = new scala.xml.PrettyPrinter(24, 4)
pp.format(books)