gpt4 book ai didi

groovy - 将更新的 XML 写入原始解析的文件

转载 作者:行者123 更新时间:2023-12-02 07:03:47 24 4
gpt4 key购买 nike

我有一个 gradle.build 我正在尝试:

  1. 读取 XML 文件
  2. 使用 XmlSlurper 更新读取的 XML 文件中的属性
  3. 将更新后的 XML 写回原始解析的 xml 文件。

仅当我将修改后的 XML 写入新的不存在 XML 文件,但写入原始解析的 XML 文件时,第三步才有效>。

将修改后的 XML 写入原始解析的 XML 文件的最简单方法是什么?

<小时/>

到目前为止我的代码:

def inFile = file('file.xml')
def outFile = file('_file.xml')

def xml = new XmlSlurper().parse(inFile)

// update xml code here

def outBuilder = new StreamingMarkupBuilder()
def outWriter = outFile.newWriter()
XmlUtil.serialize(outBuilder.bind{ mkp.yield xml }, outWriter)

我希望 outFilefile.xml,以便它覆盖原始 XML 文件。

最佳答案

如果你这样做会发生什么:

def inFile = file( 'file.xml' )
def xml = new XmlSlurper().parse( inFile )

xml.appendNode {
haha( 'tim_yates' )
}

inFile.withWriter { outWriter ->
XmlUtil.serialize( new StreamingMarkupBuilder().bind{ mkp.yield xml }, outWriter )
}

是不是没写? (似乎对我有用)

关于groovy - 将更新的 XML 写入原始解析的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18385062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com