简体   繁体   中英

XMLFormatter issue with combimation of LF and CR LF

I am doing XML Format using dom4j Outpurformat and Document objects.

I am setting lineseparator based on OS if Win -> \\r\\n and Linux -> \\n

If I pass single line xml file it working fine getting expected EOL ( CR LF ).

But If I pass multiple line xml file whatever the existing line comes with ( LF ) new lines are comes with ( CR LF )

ex : Input file :

    <root>
      <emp>
        <name>myname</name>
      </emp>
    </root>

output :

    <root> LF
    CR LF
      <emp>LF
    CR LF
        <name>myname</name>LF
    CR LF
      </emp>LF
    CR LF
    </root>LF
    CR LF

I have tried to replace the EOL still not working I tried both way

xmlContent= xmlContent.replaceAll("\n","\r\n")
xmlContent = xmlContent.replaceAll("\\n","\r\n")

Its working fine in linux as expected all time "LF", but the above mentioned problem is on windows host. its coming with combination of LF and CF LF.

String are immutable in java. So have to replace line breaks and assign to variable.

  • CR = \\r; - Carriage Return
  • LF = \\n; - Line Feed
  • CRLF = \\r\\n - End of Line

    xmlContent = xmlContent.replaceAll("\\r\\n","").repalceAll("\\n","").repalceAll("\\r","");;

It seems to me that the behavior of dom4j is correct. It just add newline (CR+LF for a windows os) while indenting output. The extra LF are coming from the original file (which i guess use the unix convention). The xml parser interprets the LF in inputs as text nodes so you get them on the output too. What if you try

xmlContent = xmlContent.replaceAll("\x0a","")

Otherwise you could configure your xml reader to ignore whitespace

SAXReader xmlReader = new SAXReader();
xmlReader.setStripWhitespaceText(true);

but this will affect the white spaces inside elements too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM