how to prevent XML Transformer from changing line endings

sinciacle

New Member
I have a method that edits an xml file. the general outline of the method is:\[code\]public void process(Path anXmlFile) { try { anXmlFile= anXmlFile.normalize(); log.debug("processing {}",anXmlFile); Document dom = buildDOM(anXmlFile.toFile()); //do stuff with dom... //delete original file //and finally ... dom.normalize(); //so we get a more predictable order Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); Source source = new DOMSource(dom); Result result = new StreamResult(anXmlFile.toFile()); transformer.transform(source, result); } catch (Exception e) { throw new IllegalStateException(e); }}\[/code\]my problem is that if i have a multi-line comment on the xml that opens in a certain line and closes in a following line (note the line break characters):\[code\]<!-- this is a long comment[cr][lf] that spans 2 lines -->\[/code\]than after I write out the modified DOM the result would be:\[code\]<!-- this is a long comment[cr] that spans 2 lines -->\[/code\]the problem is that [cr][lf] turned into [cr].this is the only part of the xml affected in this way. all other line endings are the same as the original ([cr][lf]) - even those i've modified (my code doesnt change the comment nodes in the DOM).Is there any configuration option I can give to the Transformer I create to avoid this?this is all done using JDK classes, no xml libraries involved.
 
Back
Top