肉松饼罗曼史 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方法,不要进行转换。

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程