Strip whitespace and newlines from XML in Java
问题 Using Java, I would like to take a document in the following format: <tag1> <tag2> <![CDATA[ Some data ]]> </tag2> </tag1> and convert it to: <tag1><tag2><![CDATA[ Some data ]]></tag2></tag1> I tried the following, but it isn't giving me the result I am expecting: DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); dbfac.setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.parse(new FileInputStream("/tmp