Xml or html beautifier from staring content in java. So we need one jar file please download from google
1> To use this class, you need Apache xercesImpl.jar
- package com.kartik.xml.beauty;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XmlBeautifier {
public static void main(String[] args) {
XmlBeautifier formatter = new XmlBeautifier();
String book = convertXMLFileToString("C:\\Users\\kmandal\\Desktop\\breakFirst.xml");
System.out.println(formatter.format(book));
}
/**
*
* @param fileName
* @return
*/
public static String convertXMLFileToString(String fileName) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
InputStream inputStream = new FileInputStream(new File(fileName));
org.w3c.dom.Document doc = documentBuilderFactory
.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance()
.newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
return stw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param unformattedXml
* - XML String
* @return Properly formatted XML String
*/
public String format(String unformattedXml) {
try {
Document document = parseXmlFile(unformattedXml);
OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
fileWrite(out.toString());
return out.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
/**
* This function converts String XML to Document object
*
* @param in
* - XML String
* @return Document object
*/
private Document parseXmlFile(String in) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(in));
return db.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Takes an XML Document object and makes an XML String. Just a utility
* function.
*
* @param doc
* - The DOM document
* @return the XML String
*/
public String makeXMLString(Document doc) {
String xmlString = "";
if (doc != null) {
try {
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
xmlString = sw.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return xmlString;
}
/**
*
* @param strContent
*/
public static void fileWrite(String strContent) {
BufferedWriter bufferedWriter = null;
try {
File myFile = new File(
"C:\\Users\\kmandal\\Desktop\\MyTestFile.xml");
// check if file exist, otherwise create the file before writing
if (!myFile.exists()) {
myFile.createNewFile();
}
Writer writer = new FileWriter(myFile);
bufferedWriter = new BufferedWriter(writer);
bufferedWriter.write(strContent);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null)
bufferedWriter.close();
} catch (Exception ex) {
}
}
}
}
- <?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu><food>
<name>Belgian Waffles</name>
<price>$5.95</price><description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories></food><food>
<name>Strawberry Belgian Waffles</name><price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories></food>
<food>
<name>Berry-Berry Belgian Waffles</name><price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories></food>
- </breakfast_menu>