我想把xml文件数据映射到mysql数据库中,数据库编码gb2312,程序要用到的catalog.hbm.xml和
catalog.xml文件都写了encoding="gb2312"但是数据库的中文字符仍然是显示??,请问怎么解决?附:以下是通过catalog.hbm.xml将catalog.xml映射到数据库的方法程序:
public void saveXMLDocument(File xmlDocument) {
Transaction tx = null;
Session session = null; try {
Configuration config = new Configuration(); config.addFile("Catalog.hbm.xml"); SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession(); Session dom4jSession = session.getSession(EntityMode.DOM4J); tx = session.beginTransaction(); SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlDocument); List list = document.selectNodes("//catalog");
Iterator iter = list.iterator(); while (iter.hasNext()) {
Object catalog = iter.next(); dom4jSession.save("Catalog", catalog);
} session.flush();
tx.commit();
session.close();
} catch (org.hibernate.HibernateException e) {
System.out.println(e.getMessage());
} catch (org.dom4j.DocumentException e) {
System.out.println(e.getMessage());
}
}
catalog.xml文件都写了encoding="gb2312"但是数据库的中文字符仍然是显示??,请问怎么解决?附:以下是通过catalog.hbm.xml将catalog.xml映射到数据库的方法程序:
public void saveXMLDocument(File xmlDocument) {
Transaction tx = null;
Session session = null; try {
Configuration config = new Configuration(); config.addFile("Catalog.hbm.xml"); SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession(); Session dom4jSession = session.getSession(EntityMode.DOM4J); tx = session.beginTransaction(); SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlDocument); List list = document.selectNodes("//catalog");
Iterator iter = list.iterator(); while (iter.hasNext()) {
Object catalog = iter.next(); dom4jSession.save("Catalog", catalog);
} session.flush();
tx.commit();
session.close();
} catch (org.hibernate.HibernateException e) {
System.out.println(e.getMessage());
} catch (org.dom4j.DocumentException e) {
System.out.println(e.getMessage());
}
}
catalog.hbm.xml开头是<?xml version="1.0" encoding="gb2312"?>
catalog.xml开头原来没有这个<?xml version="1.0" encoding="gb2312"?>,后来我加上了,但是两次都不能显示中文。。
public void retrieveXMLDocument() {
Transaction tx = null;
Session session = null; try {
Configuration config = new Configuration(); config.addFile("Catalog.hbm.xml"); SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession(); Session dom4jSession = session.getSession(EntityMode.DOM4J); tx = session.beginTransaction(); Document document = DocumentHelper.createDocument();
Element rootElement = document.addElement("catalogs"); String hqlQuery = "from Catalog";
List results = dom4jSession.createQuery(hqlQuery).list(); for (int i = 0; i < results.size(); i++) {
Element catalog = (Element) results.get(i);
rootElement.add(catalog);
} XMLWriter output = new XMLWriter(new FileWriter(
new File("c:/catalog/catalog.xml")));
output.write(document);
output.close();
} catch (org.hibernate.HibernateException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}