Tuesday, 2 August 2011

Extracting an XML formatted string out of a DOM object

     
import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
  public static void main(String[] argvthrows Exception {
  }

  static String getXMLString(Document xmlDocthrows Exception {
    StringWriter writer = null;
    DOMSource source = new DOMSource(xmlDoc.getDocumentElement());
    writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);
    StringBuffer strBuf = writer.getBuffer();
    return strBuf.toString();
  }
}

   
    
    
    
    
  

No comments:

Post a Comment