奇怪,我也试了一下
不会出现那样的问题啊
可能是你在读/写xml的时候
设置了有关namespace的某些东西

解决方案 »

  1.   

    代码很简单阿,没有设置namespace的东西,就是用到了递归,遍历所有的节点
    代码我贴出来
    import java.io.*;
    import java.sql.*;
    import java.util.*;import org.jdom.*;
    import org.jdom.input.*;public class XMLchinese2english {    /**
         * 带解析的xml文件流
         */
        private FileInputStream xmlStream;    /**
         * 用来存储待翻译的节点的对象
         */
        private List nodeList = new ArrayList();    /**
         * 用来存放已经翻译好的NodeVO对象,这里用Map,主要是为了效率,key:String.valueOf(hashCode),value:NodeVO对象
         */
        private Map translationMap = new HashMap();    private Connection connection;    /**
         * 构造函数
         * @param xmlStream FileInputStream 要分析的xml文件流
         * @throws Exception
         */
        public XMLchinese2english(FileInputStream xmlStream) throws Exception {
            if (xmlStream == null) {
                throw new Exception("The xml stream is null !");
            }
            this.xmlStream = xmlStream;
        }    /**
         * 构造函数
         * @param xmlFilePath String 要分析的xml文件路径
         * @throws Exception
         */
        public XMLchinese2english(String xmlFilePath) throws Exception {
            if (xmlFilePath == null) {
                throw new Exception("The xml file path is null !");
            }
            File f = new File(xmlFilePath);
            if (!f.exists()) {
                throw new Exception("The xml file not found !");
            }
            this.xmlStream = new FileInputStream(f);
        }    private void analysis(Element root) {
            List children = root.getChildren();
            //如果已经没有子节点了
            if (children == null || children.size() == 0) {
                //判断该节点是否是name或者Comment
                if ("name".equalsIgnoreCase(root.getName())
                    || "Comment".equalsIgnoreCase(root.getName())) {
                    String text = root.getText();
                    if (hasChinese(text)) {
                        if (!this.nodeList.contains(root.getText())) {
                            this.nodeList.add(root.getText());
                        }
                    }
                }
            }
            for (int i = 0; i < children.size(); i++) {
                Element modelNode = (Element) children.get(i);
                analysis(modelNode);
            }
        }    /**
         * 解析xml中固定键值,如果是中文,那么写入数据库中,等待翻译
         */
        public void xmlChinese2db() throws Exception {
            if (this.connection == null) {
                throw new Exception("The database connection is null !");
            }
            this.nodeList.clear();
            try {
                //create xml document
                SAXBuilder sb = new SAXBuilder();
                Document doc = sb.build(this.xmlStream);            //获得这个文档得root元素
                Element root = doc.getRootElement();
                analysis(root);
                writeDB();
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }    }
      

  2.   

    /**
         * 生成已经翻译好的pdm文件
         * @param outputFilePath String
         * @throws Exception
         */
        public void buildNewXML(String outputFilePath) throws Exception {
            if (outputFilePath == null) {
                throw new Exception("The output file path is null !");
            }
            File f = new File(outputFilePath);
            if (!f.exists() || !f.isFile()) {
                throw new Exception("The output file path is invalid !");
            }
            buildNewXML(new FileOutputStream(f));
        }    /**
         * 生成已经翻译好的pdm文件
         * @param outputFilePath String
         * @throws Exception
         */
        public void buildNewXML(File outputFile) throws Exception {
            if (outputFile == null) {
                throw new Exception("The output file is null !");
            }
            if (!outputFile.exists() || !outputFile.isFile()) {
                throw new Exception("The output file is invalid !");
            }
            buildNewXML(new FileOutputStream(outputFile));
        }    /**
         * 生成已经翻译好的pdm文件
         * @param outputFilePath String
         * @throws Exception
         */
        public void buildNewXML(FileOutputStream fileOutputStream) throws Exception {
            if (fileOutputStream == null) {
                throw new Exception("The output stream is null !");
            }
            if (this.connection == null) {
                throw new Exception("The database connection is null !");
            }
            this.translationMap.clear();
            getFromDB();        //create xml document
            SAXBuilder sb = new SAXBuilder();
            Document doc = sb.build(this.xmlStream);        writeXML(doc.getRootElement());        //output file
            org.jdom.output.XMLOutputter xml = new org.jdom.output.XMLOutputter("", false,
                "UTF-8");
            xml.output(doc, fileOutputStream);
        }    private void writeXML(Element root) {
            List children = root.getChildren();
            //如果已经没有子节点了
            if (children == null || children.size() == 0) {
                //判断该节点是否是name或者Comment
                if ("name".equalsIgnoreCase(root.getName())
                    || "Comment".equalsIgnoreCase(root.getName())) {
                    String text = root.getText();
                    if (hasChinese(text)) {
                        NodeVO node = (NodeVO)this.translationMap.get(text);
                        root.setText(node.getTranslation());
                    }
                }
            }
            for (int i = 0; i < children.size(); i++) {
                Element modelNode = (Element) children.get(i);
                writeXML(modelNode);
            }    }    private boolean hasChinese(String text) {
            if (text == null) {
                return false;
            }
            for (int i = 0; i < text.length(); i++) {
                if (text.charAt(i) > 255) {
                    return true;
                }
            }
            return false;
        }    //把解析出来的中文(待翻译)写入数据库
        private void writeDB() throws Exception {
            if (this.nodeList == null || this.nodeList.size() == 0) {
                return;
            }
            String checkSQL =
                "select count(*) from a_xmlchinese2english where text=?";
            String sql =
                "insert into a_xmlchinese2english(text,translation) values(?,?)";
            PreparedStatement ps = null;
            PreparedStatement psCheck = null;
            ResultSet rs = null;
            try {
                connection.setAutoCommit(false);
                ps = connection.prepareStatement(sql);
                psCheck = connection.prepareStatement(checkSQL);
                for (int i = 0; i < this.nodeList.size(); i++) {
                    String text = (String)this.nodeList.get(i);
                    psCheck.setString(1, text);
                    rs = psCheck.executeQuery();
                    int count = 0;
                    if (rs.next()) {
                        count = rs.getInt(1);
                    }
                    rs.close();
                    if (count > 0) {
                        continue;
                    }
                    ps.setString(1, text);
                    ps.setString(2, null);
                    ps.executeUpdate();
                }
                connection.commit();
            } catch (SQLException ex) {
                connection.rollback();
                throw ex;
            } finally {
                if (ps != null) {
                    ps.close();
                }
                connection.setAutoCommit(true);
            }
        }    //把解析出来的中文(待翻译)写入数据库
        private void getFromDB() throws Exception {
            String sql = "select text,translation from a_xmlchinese2english";
            Statement st = null;
            ResultSet rs = null;
            try {
                st = connection.createStatement();
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    NodeVO vo = new NodeVO();
                    vo.setText(rs.getString(1));
                    vo.setTranslation(rs.getString(2));
                    this.translationMap.put(String.valueOf(vo.getText()), vo);
                }
            } catch (SQLException ex) {
                throw ex;
            } finally {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
            }
        }    public static void main(String args[]) throws Exception {
            XMLchinese2english xml = new XMLchinese2english(new FileInputStream(
                "d:\\Endorsement.pdm"));
            Connection cn = null;
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                cn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@172.16.7.28:1521:ora28", "falcon_dev",
                    "falcon_dev");
                xml.setConnection(cn);
                xml.xmlChinese2db();
                xml.buildNewXML("d:\\test.pdm");
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (cn != null) {
                    cn.close();
                }
            }
        }    public void setConnection(Connection connection) {
            this.connection = connection;
        }}
      

  3.   

    我的思路是这样,先把文件的中文写入到数据库中,再由专门的翻译人员翻译,翻译好了,放到另外一个字段里面,然后再写会pdm文件中建表的sql
    -- Create table
    create table A_XMLCHINESE2ENGLISH
    (
      TEXT        VARCHAR2(4000) not null,
      TRANSLATION VARCHAR2(4000)
    )
    tablespace USERS
      pctfree 10
      initrans 1
      maxtrans 255
      storage
      (
        initial 64K
        minextents 1
        maxextents unlimited
      );
    -- Create/Recreate primary, unique and foreign key constraints 
    alter table A_XMLCHINESE2ENGLISH
      add constraint PK_6849764AQWERSDF primary key (TEXT)
      using index 
      tablespace USERS
      pctfree 10
      initrans 2
      maxtrans 255
      storage
      (
        initial 64K
        minextents 1
        maxextents unlimited
      );
      

  4.   

    public class PdmDemo {
        public static void main(String[] args) {
            pdmToDB();
            dbToPDM();
        }    //将pdm里的中文分析后写入数据库
        public void pdmToDB() {
            Document doc = readFromFile("test.pdm");
            List chineseWords = analysis(doc);
            writeToDB(chineseWords);
        }    //将翻译后的结果写回pdm
        public void dbToPDM() {
            List englishWordsWithChinese = readFromDB();
            Document doc = readFromFile("test.pdm");
            translate(doc, englishWordsWithChinese);
            writeToFile(doc, "test.pdm");
        }    private List analysis(Document doc) {
            //分析pdm里固定key的value,是中文则存入List中
            //返回结果List
        }    private void translate(Document doc, List englishWordsWithChinese) {
            //将List中存放的翻译结果对应写到Document对象中
        }    private Document readFromFile(String xmlFilePath) {
            //读取pdm文件,生成jdom Document对象
        }    private void writeToFile(Document doc, String xmlFilePath) {
            //把jdom Document对象写入pdm文件
        }    private List readFromDB() {
            //读取数据库,将翻译后的结果生成英-中对照的List,以便写回pdm
            //这里用List也许不是很恰当,如果无法实现,可以考虑用Map
        }    private void writeToDB(List chineseWords) {
            //将存放中文内容的List写入数据库,等待翻译
        }
    }
      

  5.   

    上面贴的是根据我理解到的流程写的一段伪代码
    基本上的思路是:
    读取pdm,生成Document->解析该Document,得到需要翻译的中文列表->将该中文列表写入数据库->翻译->读取数据库,得到中英对照的结果列表->根据该列表,对应修改Document里的中文->将修改后的Document写回pdm文件实现细节没有什么好讲述的,你应该比我清楚
    这里要提醒的是,你所描述的问题,可能就出现在修改Document这个步骤上
    我以前没怎么使用namespace,所以对其间可能发生的情况不太了解,等有空的时候再仔细看看
    你可以把修改后的Document对象变成String,输出到console,检查一下是否正确下面提供一个Document -> String的方法,仅供参考:
    public static String TranToString(Document document, String encoding) {
        ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
        XMLOutputter docWriter = new XMLOutputter("", false, encoding);
        try {
            docWriter.output(document, byteRep);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteRep.toString();
    }
      

  6.   

    试过了,打到console上,也是有xmlns:a="attribute",不清楚是为什么,但是我实际上操作(修改)文件只是用到了element.setText方法阿,其他没有用到,再一般没有namespace的xml文件上是没有这个问题,我已经试验过了.请那位大侠施以援手
      

  7.   

    楼主是不是和我同名呀?你也叫guorui,我也是呀,倒!!!!!这个分接定了,哈,谢谢楼主!
      

  8.   

    试了一下楼主的代码
    我拷了那个writeXML方法到我的程序中
    修改后的pdm也没有出现多余的a:attribute
    那么说明不是setText的时候出了差错
    聚我估计,应该是修改后的pdm中缺少原有的对a的namespace声明
    所以会在每个引用到的地方自己添加上