View Javadoc

1   package org.apache.ws.scout.util;
2   
3   import java.io.StringWriter;
4   
5   import javax.xml.transform.Source;
6   import javax.xml.transform.Transformer;
7   import javax.xml.transform.TransformerConfigurationException;
8   import javax.xml.transform.TransformerException;
9   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  	  {
31  		  String xml=null;
32  		  try {
33  		      Source source = new DOMSource(node);
34  		      StringWriter stringWriter = new StringWriter();
35  		      javax.xml.transform.Result result = new StreamResult(stringWriter);
36  		      TransformerFactory factory = TransformerFactory.newInstance();
37  		      Transformer transformer = factory.newTransformer();
38  		      transformer.transform(source, result);
39  		      xml=stringWriter.getBuffer().toString();
40  		  } catch (TransformerConfigurationException e) {
41  		      log.error("Could not obtain the XML. ", e);
42  		  } catch (TransformerException e) {
43  			  log.error("Could not obtain the XML. ",e);
44  		  }
45  		  return xml;
46  	  }
47  }