读书人

java生成xml报错!求各位帮忙!该怎么

发布时间: 2012-02-05 12:07:14 作者: rapoo

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){            }        }    } 


[解决办法]
建议使用第三方dom4j.jar
[解决办法]

探讨

是很长,能有10000行左右的数据。但是看文件中已经保存完整了啊,数据没有错误,格式也没有损坏。

[解决办法]
探讨
132187139373310.10.72.172.xml
这是文件的名称,应该不能超过长度的限制吧,是使用时间和客户端IP地址组成的名称。

[解决办法]
探讨
试过了,仍然不是这个问题,关键是把生成的xml文档转换为txt,在里面随便改点什么(如:在最后添加一个空格),在改回来xml类型的就可以正常的右键使用了,可不可能是编码格式的问题呢?

读书人网 >J2EE开发

热点推荐