package test.oracle.xml;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.w3c.dom.Document;
public class XMLTestor {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
org.xml.sax.InputSource inStream = new org.xml.sax.InputSource();
inStream.setCharacterStream(new java.io.StringReader(""));
Document doc = docBuilder.parse(inStream);
Session session = new Configuration()
.configure("hibernate.test.cfg.xml").buildSessionFactory()
.openSession();
XMLBean bean = new XMLBean();
session.getTransaction().begin();
bean.setXmlDoc(doc);
session.save(bean);
session.flush();
session.getTransaction().commit();
XMLBean loadedBean = (XMLBean) session.load(XMLBean.class, bean.getId());
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(loadedBean.getXmlDoc());
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
session.close();
}
}