我想把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());
        }
    }

解决方案 »

  1.   

    请问能说详细点么?数据库定义的时候就定义为编码gb2312了
    catalog.hbm.xml开头是<?xml version="1.0" encoding="gb2312"?>
    catalog.xml开头原来没有这个<?xml version="1.0" encoding="gb2312"?>,后来我加上了,但是两次都不能显示中文。。
      

  2.   

    是数据库中不能显示中文。另:我现在做的事只是xml中的数据和mysql相互转换,就是解析xml,数据映射到mysql中,mysql的数据也能以xml的形式再显示出来。没有涉及页面问题。
      

  3.   

    这个问题我自己解决了,在hibernate.properties里面url后加上encoding=gb2312就好了新问题是现在数据库是gb2312编码,数据库可以正常显示中文,那数据库中的数据输出成xml时怎么能显示中文呢?请指教输出xml的程序如下:
    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());
            }
        }