CodeFX-org/java-9-wtf

xml transformer issue how to fix

HaasJona opened this issue · 5 comments

Any idea how to fix/workaround the xml transformer issue so that it behaves mostly the same as in java 8 (no additional whitespace added)? A workaround might be removing the exisiting whitespace first, but I have no idea how to do that.

I have not looked into that. It is likely that our tests that discovered that behavior change are still deactivated on Java 9+ (can't check right now). If you can provide a fix, that would be highly appreciated. 😃

I found that removing unecessary whitespace when reading the file is an okay workaround for my usecase.

I did this:

		final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		factory.setValidating(true);
		factory.setAttribute("http://xml.org/sax/features/validation", Boolean.TRUE);
		factory.setIgnoringElementContentWhitespace(true); // ←!!!

when reading

and I also added

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

when writing, so that previous Java versions match the output of the new Java versions.

Do you want to add that as a workaround for your specific case to the README? PRs welcome. 😉

Unfortunately the factory.setIgnoringElementContentWhitespace(true); workaround does not work with Java 11 anymore :-(

Still works for me, I believe.