首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jdom操作xml文件出现很多奇怪的回车符?该怎么处理

2012-01-05 
jdom操作xml文件出现很多奇怪的回车符?方法如下:publicstaticvoidAddXML(DeptVOvo)throwsException{FileIn

jdom操作xml文件出现很多奇怪的回车符?
方法如下:
public   static   void   AddXML(DeptVO   vo)   throws   Exception   {
                FileInputStream   fi   =   null;
                FileOutputStream   fo   =   null;
                try   {
                        String   path   =   "1.xml ";
                        fi   =   new   FileInputStream(path);
                        SAXBuilder   sb   =   new   SAXBuilder();//建立构造器
                        Document   doc   =   sb.build(fi);
                        Element   root   =   doc.getRootElement();//得到根元素
                        List   roots   =   root.getChildren();//得到根元素所有子元素的集合

                        int   deptno   =   vo.getDeptno();
                        String   dname   =   vo.getDname();
                        String   loc   =   vo.getLoc();

                        Element   newroot   =   new   Element( "dept ");

                        Element   newdeptno   =   new   Element( "deptno ");
                        newdeptno.setText(Integer.toString(deptno));
                        newroot.addContent(newdeptno);

                        Element   newdname   =   new   Element( "dname ");
                        newdname.setText(dname);
                        newroot.addContent(newdname);

                        Element   newloc   =   new   Element( "loc ");
                        newloc.setText(loc);
                        newroot.addContent(newloc);

                        roots.add(newroot);//增加子元素

                        String   indent   =   "   ";
                        boolean   newLines   =   true;
                        XMLOutputter   out   =   new   XMLOutputter(indent,   newLines);


                        fo   =   new   FileOutputStream(path);
                        out.output(doc,   fo);
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                }   finally   {
                        try   {
                                fi.close();
                                fo.close();
                        }   catch   (Exception   ex)   {
                                ex.printStackTrace();
                        }
                }
        }

xml文件:
1.xml
<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<root>


        <dept>


                <deptno> 10 </deptno>


                <dname> ACCOUNTING </dname>


                <loc> NEW   YORK </loc>


        </dept>


      <dept>


            <deptno> 20 </deptno>


            <dname> RESEARCH </dname>


            <loc> DALLAS </loc>


      </dept>


    <dept>

        <deptno> 30 </deptno>

        <dname> SALES </dname>

        <loc> CHICAGO </loc>

    </dept>

  <dept>
    <deptno> 40 </deptno>
    <dname> OPERATIONS </dname>
    <loc> BOSTON </loc>
  </dept>
</root>


[解决办法]
太长了....哪有那么多精力看啊

热点排行