Java 1.6: javax.xml.transform.Transformer refuses to indent xml strings which contain newlines

懵懂的女人 提交于 2019-12-06 08:10:30

I've concluded that this is normal behavior for Transformer. Even more. It's indent functionality is not meant to be used as a pretty printer, not on it's own anyways. When XML is pretty printed it's structure changes unless you know exactly what the document should look like (based on it's XSD, DTD or something similar). That is the only way to determine which newline characters are to be considered ignorable whitespace and which are actual element values or a part of them. Transformer does not reformat existing whitespace and that's why the output of my code is what it is.

So if you want to pretty print an already pretty printed XML string using Transformer or any other class, you first have to get rid of ignorable whitespace and the only way to safely do that is to know what the structure of your XML document should be like. I'd like someone to confirm this statement for me since this is currently only my assumption. If this statement is correct; how do third party pretty printers do it? I know JTidy did not require an XSD, but pretty printed anyway. Does it simply treat all whitespace as ignorable whitespace unless it is enclosed in a text XML node? Are there other methods of determining and eliminating ignorable whitespace?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!