本文介绍了在 Groovy 中将 XML 转换为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 groovy 将 xml 转换为 JSON.我知道转换的细节取决于我的偏好,但是有人可以推荐我应该使用哪些库和方法,并为我提供一些关于为什么/如何使用它们的信息吗?我正在使用 groovy,因为我被告知它是一个非常有效的解析器,所以我正在寻找可以利用这一点的库

I wish to convert xml to JSON using groovy. I understand the specifics of conversion is dependent on my preferences, but could someone please recommend which libraries and methods I should be using and provide me with a little information on why/how to use them? I am using groovy as I have been told it is a very effective parser, so I am looking for libraries that will take advantage of this

谢谢!

推荐答案

你可以用基本的 Groovy 做到这一切:

You can do it all with basic Groovy:

// Given an XML string
def xml = '''<root>
            |    <node>Tim</node>
            |    <node>Tom</node>
            |</root>'''.stripMargin()

// Parse it
def parsed = new XmlParser().parseText( xml )

// Convert it to a Map containing a List of Maps
def jsonObject = [ root: parsed.node.collect {
  [ node: it.text() ]
} ]

// And dump it as Json
def json = new groovy.json.JsonBuilder( jsonObject )

// Check it's what we expected
assert json.toString() == '{"root":[{"node":"Tim"},{"node":"Tom"}]}'

然而,你真的需要考虑某些事情......

HOWEVER, you really need to think about certain things...

  • 您将如何表示属性?
  • 您的 XML 是否包含 textwoo</another>text</node> 样式标记?如果是这样,您将如何处理?
  • 数据?评论?等?
  • How are you going to represent Attributes?
  • Will your XML contain <node>text<another>woo</another>text</node> style markup? If so, how are you going to handle that?
  • CDATA? Comments? etc?

这两者之间不是一个平滑的 1:1 映射...但是对于给定的特定格式的 XML,有可能想出一个给定的特定格式的 Json.

It's not a smooth 1:1 mapping between the two... But for a given specific format of XML, it may be possible to come up with a given specific format of Json.

要从文档中获取名称(请参阅评论),您可以执行以下操作:

To get the names from the document (see comment), you can do:

def jsonObject = [ (parsed.name()): parsed.collect {
  [ (it.name()): it.text() ]
} ]

更新 2

您可以通过以下方式添加对更大深度的支持:

Update 2

You can add support for greater depth with:

// Given an XML string
def xml = '''<root>
            |    <node>Tim</node>
            |    <node>Tom</node>
            |    <node>
            |      <anotherNode>another</anotherNode>
            |    </node>
            |</root>'''.stripMargin()

// Parse it
def parsed = new XmlParser().parseText( xml )

// Deal with each node:
def handle
handle = { node ->
  if( node instanceof String ) {
      node
  }
  else {
      [ (node.name()): node.collect( handle ) ]
  }
}
// Convert it to a Map containing a List of Maps
def jsonObject = [ (parsed.name()): parsed.collect { node ->
   [ (node.name()): node.collect( handle ) ]
} ]

// And dump it as Json
def json = new groovy.json.JsonBuilder( jsonObject )

// Check it's what we expected
assert json.toString() == '{"root":[{"node":["Tim"]},{"node":["Tom"]},{"node":[{"anotherNode":["another"]}]}]}'

同样,之前所有的警告仍然适用(但此时应该听到更大声);-)

Again, all the previous warnings still hold true (but should be heard a little louder at this point) ;-)

这篇关于在 Groovy 中将 XML 转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:54