I use jdom SAXBuilder to get a XML document. My problem is that in the document returned, every element is preceded by a line separator ('/n'). I looked at jdom docs and seems that this is the normal behavior of the parser. I'd like to avoid it but I can not find the way. Any hint would be appreciated. Flavio
1 Answers
Where are you seeing the \n characters? If they are part of the input then JDOM will automatically keep them and when you output the JDOM content they will still be there. If you want to change how the data is output, you can use a 'Format' instance to change the output format.
If you want the output to have no \n characters, then you should use the Format.getCompactFormat() format... but note - if you are using the new JDOM 2.0.0 this has a bug, and you should use the hotfix zip on github: https://github.com/downloads/hunterhacker/jdom/jdom-2.x-issue72.zip . JDOM-2.0.1 will be released in the next week or so with the official fix.
If you want to strip out the \n characters as part of the input/parse process, then you should consider using your own JDOMFactory instance (subclass DefaultJDOMFactory) and override the text(...) method to trim the text before returning the Text instance.
Otherwise, after parsting the XML, you can scan the entire tree and stip the \n characters as needed.
Otherwise, JDOM does not add \n characters unless you ask it to.

- 17,539
- 7
- 42
- 76