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

The following examples show how to use com.sun.org.apache.xml.internal.serialize.XMLSerializer#serialize() . 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: CustomSchemaParser.java    From rice with Educational Community License v2.0 6 votes vote down vote up
protected String nodeToString(Node node) {
    StringWriter stringOut = new StringWriter();

    OutputFormat format = new OutputFormat(Method.XML, null, false);
    format.setOmitXMLDeclaration(true);

    XMLSerializer serial = new XMLSerializer(stringOut, format);

    try {
        serial.serialize(node);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return stringOut.toString();
}
 
Example 4
Source File: XmlReport.java    From livingdoc-core with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void printTo(Writer out) throws IOException {
    if (dom == null) {
        return;
    }

    StringWriter sw = new StringWriter();

    OutputFormat format = new OutputFormat(dom);
    format.setIndenting(true);

    XMLSerializer serializer = new XMLSerializer(sw, format);
    serializer.serialize(dom);

    out.write(sw.toString());
    out.flush();
}
 
Example 5
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);
}