在Groovy中对XML进行排序 [英] Sorting XML in Groovy
本文介绍了在Groovy中对XML进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看过有关使用Groovy对XML进行排序的文档
I have looked at the documentation on sorting XML with Groovy
def records = new XmlParser().parseText(XmlExamples.CAR_RECORDS)
assert ['Royale', 'P50', 'HSV Maloo'] == records.car.sort{ it.'@year'.toInteger() }.'@name'
但是我想做的是对XML进行排序,然后返回已排序的xml字符串.我知道排序完成后就可以完全重建XML.
but what I am trying to do is sort the XML and then return the xml string sorted. I know I can completely rebuild the XML after i am done sorting.
我知道我可以对XML进行XML转换以对其进行排序
I know I can run an XML Transformation on the XML to get it sorted
def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(new StringReader(xslt)))
transformer.transform(new StreamSource(new StringReader(input)), new StreamResult(System.out))
但是我正在寻找一些Groovy魔术来使我更容易
BUT I was looking for some Groovy magic to make it easier for me
推荐答案
一种解决方案是直接替换 records
中的 car
列表.不确定是否存在更多魔法!
A solution is to replace directly the list of car
within the records
. Not sure if more magic exists!
records.value = records.car.sort{ it.'@year'.toInteger() }
println XmlUtil.serialize(records)
这篇关于在Groovy中对XML进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文