Java Code Examples for com.sun.org.apache.xml.internal.serialize.OutputFormat#setIndent()

The following examples show how to use com.sun.org.apache.xml.internal.serialize.OutputFormat#setIndent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextUtil.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
public String xmlFilePrint(String fileName) 
 {
    try {
        File file = new File(fileName);
        DocumentBuilderFactory documentBuilderFactory =
        DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder;
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        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);
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 2
Source File: XmlUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Format the given string as xml content.
 * @param xml
 * @return
 */
public static String formatXml(String xml) {
	try {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
		InputSource is = new InputSource(new StringReader(xml));
		Document doc = domBuilder.parse(is);
		OutputFormat format = new OutputFormat(doc);
		format.setLineWidth(80);
		format.setIndent(2);
		format.setIndenting(true);
		StringWriter out = new StringWriter();
		XMLSerializer xmls = new XMLSerializer(out, format);
		xmls.serialize(doc);
		return out.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return xml;
}
 
Example 3
Source File: VdPreview.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @return a format object for XML formatting.
 */
@NonNull
private static OutputFormat getPrettyPrintFormat() {
    OutputFormat format = new OutputFormat();
    format.setLineWidth(120);
    format.setIndenting(true);
    format.setIndent(4);
    format.setEncoding("UTF-8");
    format.setOmitComments(true);
    return format;
}
 
Example 4
Source File: XmlUtils.java    From Doradus with Apache License 2.0 5 votes vote down vote up
static public String formatXml(String xmlText, String prefix)
throws Exception
{
	if (xmlText == null)
		return xmlText;
	
	Document doc = parseXml(xmlText);
	
	OutputFormat format = new OutputFormat(doc);
       format.setLineWidth(120);
       format.setIndenting(true);
       format.setIndent(4);
       
       Writer out = new StringWriter();
       XMLSerializer serializer = new XMLSerializer(out, format);
       serializer.serialize(doc);
       xmlText = StringUtils.trim(out.toString(), " \r\n");

	if (xmlText.startsWith("<?xml")) {
		int ind = xmlText.indexOf("?>");
		if (ind > 1) {
			xmlText = xmlText.substring(ind + 2);
	        xmlText = StringUtils.trim(xmlText, " \r\n");
		}
	}
       
       return StringUtils.formatText(xmlText, prefix);
}