日期:2014-05-20  浏览次数:20700 次

java生成xml报错!!!急急急!!求各位帮忙!
使用代码创建了一个文件夹和一个xml文件,但是生成的xml文件不能点击右键(文件夹也不能点击右键),会出现系统错误。即使程序关闭,也不能点击,必须关掉服务器,估计可能没有完全释放掉占用的资源,但不知道错在了哪里。求各位大侠帮帮忙,小弟先谢谢了!

  TransformerFactory tf = TransformerFactory.newInstance();
OutputStreamWriter pw = null;
FileOutputStream fs = null;
StreamResult result = null;
try {
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
  fs = new FileOutputStream(strXmlPath);//strXmlPath为路径
pw = new OutputStreamWriter(fs,"utf-8");
result = new StreamResult(pw);
transformer.transform(source, result);
fs.flush();
pw.flush();
fs.close();
pw.close();
}
catch (Exception ex) {
}finally{
try{
if(null!=fs){
fs.close();
}
}catch(Exception ex){
}

try{
if(null!=pw){
pw.close();
}
}catch(Exception ex){
}
}

------解决方案--------------------
表示没遇到过这种问题,也没发现楼主贴出来的代码存在没有关闭的流,而且,就算文件在使用中也不会连右键都不能点啊。
------解决方案--------------------
使用了第三方的类库?是不是第三方的类库也需要关闭
特别是 
result = new StreamResult(pw);
transformer.transform(source, result); 
这两行,看看有没有对应的关闭方法
------解决方案--------------------
你确定 是你这段程序的问题? 这段程序应该不会引起那个问题吧。。。检查一下是否是与之关联的程序片段引起的。。。

可以看看这里的应用
http://www.it.com.cn/f/edu/049/20/26165_2.htm
------解决方案--------------------
Java code

刚才我随便写了一个写xml的类。。没有你说的情况。。test.xml可以右键也可正常打开

// 测试LZ问题
    public static void writeXMLFile() throws Exception {
        Document document = null;
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory
                .newDocumentBuilder();
        // 下面是建立XML文档内容的过程
        document = documentBuilder.newDocument(); 
        Element root = document.createElement("webdisknodes");
        // 根元素添加上文档
        document.appendChild(root); 
        
        // 建立node元素,添加到次级根结点
        Element resultNode = document.createElement("node");
        root.appendChild(resultNode);

        // name
        Element method = document.createElement("test");
        resultNode.appendChild(method);
        Text tMethod = document.createTextNode("测试第一个节点");
        method.appendChild(tMethod);
        Element methodArgs = document.createElement("second");
        resultNode.appendChild(methodArgs);
        Text tMethodArgs = document.createTextNode("测试第二个节点");
        methodArgs.appendChild(tMethodArgs); 

        TransformerFactory tfactory = TransformerFactory.newInstance();
        // 对document对象调用normalize(),去掉xml文档中格式
        Transformer transformer = tfactory.newTransformer(); 
        document.normalize();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File("f:/test.xml"));
        transformer.transform(source, result);
        System.out.println("success");
    }

------解决方案--------------------
引用
创建文件夹没有问题。。是否生成xml文件代码前后还有其他线程在运行或操作没有关闭。。
难道和xml文件目录及文件的权限有关?

------解决方案--------------------
sounds strange。
问一下,strXmlPath是不是很长?
------解决方案--------------------
Java code

TransformerFactory tf = TransformerFactory.newInstance();
        OutputStreamWriter pw = null;
        FileOutputStream fs = null;
        StreamResult result = null;
        String strXmlPath="c:/testXML.xml";
        try {
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource();
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            fs = new FileOutputStream(strXmlPath);//strXmlPath为路径
            pw = new OutputStreamWriter(fs,"utf-8");
            result = new StreamResult(pw);
            transformer.transform(source, result);
            fs.flush();
            pw.flush();
            fs.close();
            pw.close();
        }
        catch (Exception ex) {
        }finally{
            try{
                if(null!=fs){
                    fs.close();
                }
            }catch(Exception ex){
            }

            try{
                if(null!=pw){
                    pw.close();
                }
            }catch(Exception ex){
            }
        }
    }