@Test public void shouldCloseEmptyTags() throws Exception { File xmlFile = createXmlFile("<items></items>"); String xml = XMLUtil.loadAndNormalizeXML(xmlFile); assertEquals("<items/>", xml); }
public static String loadAndNormalizeXML(File xmlFile) throws ParserConfigurationException, SAXException, IOException, TransformerException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); removeRecursively(doc); doc.normalizeDocument(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter normalizedXml = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(normalizedXml)); return normalizedXml.toString(); }
XMLUtil { public static String loadAndNormalizeXML(File xmlFile) throws ParserConfigurationException, SAXException, IOException, TransformerException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); removeRecursively(doc); doc.normalizeDocument(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter normalizedXml = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(normalizedXml)); return normalizedXml.toString(); } }
XMLUtil { public static String loadAndNormalizeXML(File xmlFile) throws ParserConfigurationException, SAXException, IOException, TransformerException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); removeRecursively(doc); doc.normalizeDocument(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter normalizedXml = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(normalizedXml)); return normalizedXml.toString(); }  }
XMLUtil { public static String loadAndNormalizeXML(File xmlFile) throws ParserConfigurationException, SAXException, IOException, TransformerException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); removeRecursively(doc); doc.normalizeDocument(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter normalizedXml = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(normalizedXml)); return normalizedXml.toString(); }  static String loadAndNormalizeXML(File xmlFile); static T loadXML(Class<T> type, File sourceFile); static T loadXML(Class<T> type, String schemaLocation, File sourceFile); }
XMLUtil { public static String loadAndNormalizeXML(File xmlFile) throws ParserConfigurationException, SAXException, IOException, TransformerException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); removeRecursively(doc); doc.normalizeDocument(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter normalizedXml = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(normalizedXml)); return normalizedXml.toString(); }  static String loadAndNormalizeXML(File xmlFile); static T loadXML(Class<T> type, File sourceFile); static T loadXML(Class<T> type, String schemaLocation, File sourceFile);  }