01: package org.apache.ws.scout.util;
02:
03: import java.io.StringWriter;
04:
05: import javax.xml.transform.Source;
06: import javax.xml.transform.Transformer;
07: import javax.xml.transform.TransformerConfigurationException;
08: import javax.xml.transform.TransformerException;
09: import javax.xml.transform.TransformerFactory;
10: import javax.xml.transform.dom.DOMSource;
11: import javax.xml.transform.stream.StreamResult;
12:
13: import org.apache.commons.logging.Log;
14: import org.apache.commons.logging.LogFactory;
15: import org.w3c.dom.Node;
16:
17: /**
18: * Utilies for covertion between w3c formats and XML.
19: * @author Kurt Stam (kurt.stam@jboss.com)
20: */
21: public class XMLUtils {
22: private static Log log = LogFactory.getLog(XMLUtils.class);
23:
24: /**
25: * Convert a org.w3c.dom.Node into an XML representation.
26: * @param node - containing an xml tree.
27: * @return - String containing the XML.
28: */
29: public static String convertNodeToXMLString(Node node) {
30: String xml = null;
31: try {
32: Source source = new DOMSource(node);
33: StringWriter stringWriter = new StringWriter();
34: javax.xml.transform.Result result = new StreamResult(
35: stringWriter);
36: TransformerFactory factory = TransformerFactory
37: .newInstance();
38: Transformer transformer = factory.newTransformer();
39: transformer.transform(source, result);
40: xml = stringWriter.getBuffer().toString();
41: } catch (TransformerConfigurationException e) {
42: log.error("Could not obtain the XML. ", e);
43: } catch (TransformerException e) {
44: log.error("Could not obtain the XML. ", e);
45: ;
46: }
47: return xml;
48: }
49: }
|