我有一个XML,其标记值如下所示:
<ProjectNote>
<Note><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.3790.4944" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<P>Key Deliverables</P>
<UL>
<LI>schedule development
<LI>scope development (SOW)
<LI>business case (depending on project)
<LI>contracts (who will be used)
<LI>overall budget
<LI>Assign Key Stakeholders
<LI>Sitewalks and PreCon Meetings
<LI>Need Clearance?</LI></UL>
<P>&nbsp;</P></BODY></HTML>
</Note>
<ProjectNote>
我正在使用groovy脚本读取此文件并对其进行一些更改并将其写回文件。但是,在使用XmlSluper解析文件时,"
将转换为"
。除了向其添加新节点之外,我不想更改文件的任何其他部分。如何保存文件原样?
我正在使用以下代码:
package test
import groovy.xml.*
/**
* A Simple Example that searches information from XML parsed by XmlSlurper.
*/
class Test {
static srcXMLPath = "C:/SRC_Project/628548_C453_Original.xml"
static updXMLPath = "C:/SRC_Project/628548_C453_Updated.xml"
static def writer
static main(args) {
File srcFile = new File(srcXMLPath)
def baseXMLStr = new XmlSlurper(false,false).parse(srcFile)
def newXMLStr = new groovy.xml.StreamingMarkupBuilder().bind {
List_Wrapper {
mkp.yield baseXMLStr
}
}
writer = new FileWriter(updXMLPath)
groovy.xml.XmlUtil.serialize( newXMLStr,writer )
writer.close()
}
}
然而,更新的文件已更改为此,这不是我的意图:
<ProjectNote>
<Note><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.3790.4944" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<P>Key Deliverables</P>
<UL>
<LI>As Builts (if needed)
<UL>
<LI>Mapping &amp; Design Drawings</LI></UL>
<LI>Engineer needs final approval
<P>&nbsp;</P></BODY></HTML>
</Note>
<ProjectNote>
有人能告诉我如何避免它。它显然没有改变其他转义字符
答案 0 :(得分:0)
您可以在构建标记后修复它:
new File(updXMLPath) << XmlUtil.serialize(newXMLStr).replaceAll('"', '"')
但是如果你只想添加包装器,那么你不需要解析源xml,你可以将源文件过去标记为:
def newXMLStr = new StreamingMarkupBuilder().bind {
List_Wrapper {
mkp.yieldUnescaped srcFile.text
}
}
最后,如果您只需要放置一个List_Wrapper标记,那么最好这样做:
new File(updXMLPath) << "<List_Wrapper>${new File(srcXMLPath).text}</List_Wrapper>"