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

发布于 2021-11-13 00:50:07 字数 3160 浏览 823 评论 1

现在发现问题:

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

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

即时缩进严重不靠谱

//加载
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>
第二行注释部分没有换行

倒数第二行标签变了样子

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2021-11-14 23:44:41

你修改这個XML文件的代码是怎么写的,能贴出来看看么?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文