肉松饼罗曼史 2017-08-28 11:10 采纳率: 42.9%
浏览 741

java写入xml出现错误,求大神帮忙看下。

java写入xml数据,在其他电脑上运行这方法是正常的,但是在自己电脑上就运行错误了,找了很久没有找出啥原因,下午大神帮帮忙。
错误在((org.apache.crimson.tree.XmlDocument)document).write(fos);这句代码,不知道为什么。
异常如下图1:
图片说明
源码如下:

 /**
 * 修改属性值
 * @param fileName
 * @param nodeName
 * @param update
 * @throws InterruptedException 
 * @throws IOException 
 */
public static void UpdataXml(String fileName,String nodeName,String update) throws IOException, InterruptedException{
    String path = System.getProperty("user.dir") + "\\params\\"+fileName+".xml";
    FileReader fr = new FileReader(path);  
//  if (fr.read()==-1) {
        System.out.println("新建文件....");
        Deal.CreateXml("data", nodeName, fileName);
//  }
//  }else if(fr.read()!=-1 ) {
        System.out.println("文件内容不为空");
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    try{
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document document=db.parse(path);
        Node root=document.getDocumentElement(); 
        System.out.println(root);
        //判断文件内容是否为空
        NodeList nodeList = root.getChildNodes(); 
          for (int i=0;i<nodeList.getLength();i++) { 
           Node subnode = nodeList.item(i); 
              if (subnode.getNodeName()==nodeName) {       
                subnode.getFirstChild().setNodeValue(update);
          }
              System.out.println("节点已经更改");
      }

          //写入数据
       System.err.println("数据保存地址为:"+path);
          FileOutputStream fos=new FileOutputStream(new File(path));
          try {
              ((org.apache.crimson.tree.XmlDocument)document).write(fos);
              System.err.println("程序已将数据保存");
              fos.flush();
              fos.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
    }catch (Exception e) {
        // TODO: handle exception
    }

}

  • 写回答

1条回答

  • 毕小宝 博客专家认证 2017-08-29 01:06
    关注

    问题在这行代码((org.apache.crimson.tree.XmlDocument)document).write(fos);
    直接用document的相关方法,.操作看看这个document对象有哪些write方法,不要进行转换。

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛