dom4j读取xml,对这个xml修改后写入一个新的文件

wien 发布于 2011/06/01 01:51
阅读 2K+
收藏 0

现在发现问题:

新写入的这个文件,对比就文件发现在代码格式上有区别

我现在想做到修改的地方修改,没修改的地方不需要修改~

即时缩进严重不靠谱

//加载
SAXReader reader = new SAXReader();  
Document document = reader.read(new File(fileName));
//打印了一下发现这个时候似乎格式已经不对了
System.out.print(document.asXML()); 
//对document 进行修改,此时忽略
 //写入一个文件(此处都没敢使用格式)
XMLWriter writer = new XMLWriter(new FileOutputStream(destFile));
writer.write(document);
writer.close();

原来的xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2008 rel. 2 sp2 (http://www.altova.com) by xdowns.com (xdowns.com) -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:lang="http://www.springframework.org/schema/lang"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- Service -->
  <bean id="baseService" class="com.test.test" >
                              <property name="encoding" 
                              value="UTF-8" />
                    </bean>
  <bean></bean>
</beans>
生成后的XML:

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2008 rel. 2 sp2 (http://www.altova.com) by xdowns.com (xdowns.com) --><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:lang="http://www.springframework.org/schema/lang"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- Service -->
  <bean id="baseService" class="com.test.test" >
                              <property name="encoding" 
                              value="UTF-8" />
                    </bean>
  </bean/>
</beans>
第二行注释部分没有换行

倒数第二行标签变了样子

加载中
0
苗哥
苗哥
你修改这個XML文件的代码是怎么写的,能贴出来看看么?
返回顶部
顶部