可以使用XmlSlurper
或XmlParser
完成XML转换。但我正在寻找其他解决方案。因为,我可能有大小超过1 GB的XML文件,SAX Parser可能无法处理它。
INPUT:(Before transformation)
<response version-api="2.0">
<value>
<ErrorCodes>1, 2, 3, 4</ErrorCodes>
</value>
</response>
OUTPUT:(After Transformation)
<response version-api='2.0'>
<value>
<ErrorCode>1</ErrorCode>
<ErrorCode>2</ErrorCode>
<ErrorCode>3</ErrorCode>
<ErrorCode>4</ErrorCode>
</value>
</response>
答案 0 :(得分:1)
Groovy API使用了懒惰的评估,但是对于1GB或更大的XML文件,您应该考虑StAX。它不是像SAX那样回调驱动,而是一个使用迭代器的流API,它为您提供了更多的编写代码的灵活性。
再次查看您的示例,您还可以从使用常规StreamingMarkupBuilder
或MarkupBuilder
类中获益。对于像这样的大型文档,前者应该更好。它们非常易于使用,是您将转换逻辑与StAX混合的绝佳方式。
答案 1 :(得分:0)
这是一个简单的脚本,它提到了转换问题。
请在内容中找到评论:
import groovy.xml.XmlUtil
def xml = '''<response version-api="2.0">
<value>
<ErrorCodes>1, 2, 3, 4</ErrorCodes>
</value>
</response>'''
def newXml = new XmlSlurper().parseText(xml)
//Get the current Error codes into a list
def codes = newXml.value.ErrorCodes.toString().split(',')*.trim()
//remove the existing ErrorCodes node
newXml.value.ErrorCodes.replaceNode {}
//Create the transformed xml by adding the list of ErrorCodes
newXml.value.appendNode {
codes.each {
ErrorCodes(it)
}
}
println XmlUtil.serialize(newXml)
您可以尝试groovy web console
中的脚本更新:
我只是在解决这个问题中的拼写错误。
看起来用户不想使用XmlSlurper
?后来实现了。
另一种方法是使用stylesheet
进行转换。
可能你可以尝试用不同的方式花多少时间。
找到几个链接:
java
代码。