当使用Groovy XmlParser更新时,Xml变空白
问题描述:
我对groovy颇为陌生并寻求您的帮助。 我想读取增量xml文件并根据名称属性更新主xml文件。这两个xml文件都具有相同的结构。我正在尝试更新主XML文件中的属性值。但主文件一次空白,文件被更新。我不确定我错在哪里。当使用Groovy XmlParser更新时,Xml变空白
下面是XML结构:
<item-descriptor name="user" cache-mode="simple" item-cache-size="3000" query-cache-size="1000" item-cache-timeout="900000" query-expire-timeout="60000" />
<item-descriptor name="contactInfo" cache-mode="simple" item-cache-size="10000" query-cache-size="1000" item-cache-timeout="900000" query-expire-timeout="60000" />
下面是该代码:
def templatexmlConfig = new XmlParser().parse(templateConfigFile)
def basexmlConfig = new XmlSlurper().parse(baseConfigFile)
def templateItemDesNode = templatexmlConfig.'item-descriptor'
def baseItemDesNode=basexmlConfig.'item-descriptor'
templateItemDesNode.each()
{
Map bindings=[:]
def nameAttr=it.attribute('name')
it.attributes().each{attrName,attrValue->
if(!attrName.equals('name'))
{
bindings.put(attrName,attrValue)
}}
if(baseItemDesNode.find{ [email protected]().equals(nameAttr)}.size()!=0)
{
bindings.each
{
def a=it.key
def v=it.value
baseItemDesNode.find{ [email protected]().equals(nameAttr)}[email protected]'a'="${v}" }
}
}
new XmlNodePrinter(new PrintWriter(outputFile)).print(basexmlConfig)
答
好吧,给出了两个示例XML文件:
def templateXml = '''<xml>
| <item-descriptor name="a" cache-mode="r1" item-cache-size="1"/>
| <item-descriptor name="b" cache-mode="r2" item-cache-size="2" new-attr="tim"/>
| <item-descriptor name="z" cache-mode="r3" item-cache-size="3"/>
|</xml>'''.stripMargin() ;
def baseXml = '''<xml>
| <item-descriptor name="b" cache-mode="o1" item-cache-size="10"/>
| <item-descriptor name="c" cache-mode="o2" item-cache-size="11"/>
| <item-descriptor name="a" cache-mode="o3" item-cache-size="12"/>
|</xml>'''.stripMargin()
我们可以分析这些(对于两者都使用XmlParser,你有一个解析器,另一个使用slurper):
def templatexmlConfig = new XmlParser().parseText(templateXml)
def basexmlConfig = new XmlParser().parseText(baseXml)
然后拿到item-descriptor
节点(如你有它)
def templateItemDesNode = templatexmlConfig.'item-descriptor'
def baseItemDesNode = basexmlConfig.'item-descriptor'
然后,遍历模板item-descriptors
,生成地图一个无名的属性(使用findAll
比循环中,您更容易有),并使用相同的名称替换节点上的所有节点baseXml
:
templateItemDesNode.each() { tidn ->
Map bindings = tidn.attributes().findAll { it.key != 'name' }
def nameAttr = [email protected]
baseItemDesNode.find { b -> [email protected] == nameAttr }?.with { node ->
bindings.each { a, v ->
[email protected]"$a" = v
}
}
}
而对于这一点很容易运行的例子,只是打印出来的Xml :
println new StringWriter().with { sw ->
new XmlNodePrinter(new PrintWriter(sw)).print(basexmlConfig)
sw.toString()
}
而且在运行时,上面打印出:
<xml>
<item-descriptor name="b" cache-mode="r2" item-cache-size="2" new-attr="tim"/>
<item-descriptor name="c" cache-mode="o2" item-cache-size="11"/>
<item-descriptor name="a" cache-mode="r1" item-cache-size="1"/>
</xml>
显然,在你工作的代码,你需要回去使用parse
在XmlParsers
解析您的文件,而不是字符串我在这里,并将输出改回写入文件...
希望它有帮助!
谢谢您的澄清和代码蒂姆。我将对代码进行更改并对其进行测试。会让你。谢谢。 – user1470220
@ user1470220酷。手指交叉:-) –
谢谢蒂姆。我已经试过你的一段代码,它的工作原理非常完美。非常感谢。您能否给我推荐一本适合Groovy的好书?我是一名初学者,希望打造我的基础知识。 – user1470220