Java Code Examples for org.apache.xml.serialize.XMLSerializer#setNamespaces()

The following examples show how to use org.apache.xml.serialize.XMLSerializer#setNamespaces() . 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: XmlSerializer.java    From cosmo with Apache License 2.0 6 votes vote down vote up
public static String serialize(XmlSerializable serializable)
    throws IOException {
    StringWriter out = new StringWriter();
    try {
        Document doc = BUILDER_FACTORY.newDocumentBuilder().newDocument();
        doc.appendChild(serializable.toXml(doc));
        
        OutputFormat format = new OutputFormat("xml", "UTF-8", true);
        XMLSerializer serializer =
            new XMLSerializer(out, format);
        serializer.setNamespaces(true);
        serializer.asDOMSerializer().serialize(doc);

        return out.toString();
    } catch (ParserConfigurationException e) {
        throw new CosmoParseException(e);
    }
}
 
Example 2
Source File: XMLUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/** 
 * Element to String without format
 * 
 * @param elto to serialize
 * @return serialized elto
 */
public static String elementToString(Element elto) {
	StringWriter writer = new StringWriter();
	OutputFormat of = new OutputFormat();
	of.setOmitXMLDeclaration(true);
	XMLSerializer serializer = new XMLSerializer(writer, of);
	serializer.setNamespaces(true);
	try {
		serializer.serialize(elto);
	} catch (IOException ioe) {
		log.error(ioe);
	}
	return writer.toString();
}