用grails制作XML是一个好方法 [英] Whats a good way to make XML with grails

查看:73
本文介绍了用grails制作XML是一个好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的谷歌搜索看起来像你应该使用MarkupBuilder,但我不明白。看起来我可以像XML一样执行 import grails.converters.XML ,但这并不能真正给我想要的。



我想要:

 < Thingie> 
< someValue> blah< / someValue>
< hellaItems>
<项目>
< anotherValue> yaddayadda< / anotherValue>
< / Item>
<项目>
< anotherValue>不同于第一< / anotherValue>
< / Item>
< / hellaItems>

< / Thingie>

我甚至不知道从哪里开始......



@Stefan如果我想动态地执行操作,该怎么办?我不认为我理解建设者一般可能是问题。

  def items = [yaddayadda,不同于第一个] 

更新:看起来像我越来越近,但有人可以帮助我最后部分。我这样做:

  def items = [yaddayadda,与第一个不同)
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.thingie(){
someValue('blah')
hellaItems(){
items .each {
item(){
anotherValue(it)
}
}


}
}
def xmlString = writer.toString()
println也许这只会工作
println xmlString

打印:

 也许这只会起作用
< thingie>
< someValue> blah< / someValue>
< hellaItems>
< item>
< anotherValue />
< / item>
< item>
< anotherValue />
< / item>
< / hellaItems>
< / thingie>

为什么我的 anotherValue 的那么?

更新:使用下面的tmpHolder求解,但Bill有更好的语法建议。



<$ p $
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
$ def $ = $ {code> def items = [yaddayadda,与第一个不同) xml.thingie(){
someValue('blah')
hellaItems(){
items.each {
def tmpHolder = it
item(){
anotherValue(tmpHolder)
}
}


}
}
def xmlString = writer.toString()
println也许这只会起作用
println xmlString


解决方案

  import groovy.xml.MarkupBuilder 

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml (){
someValue('blah')
hellaItems(){
item(){
anotherValue('yaddayadda')
}
item(){
anotherValue('不同于第一个')
}
}
}

writer.toString()



你没有得到什么?语法有点奇怪,但那是因为它是DSL。它不应该看起来像正常的groovy代码。 作为XML 的工作原理完全不同,除非您的对象图匹配您准确发布的XML,否则您将无法得到您想要的结果。


Brief googling looks like you're supposed to use "MarkupBuilder," but I don't understand it. It looks like I can do "as XML" having done import grails.converters.XML but this is not really giving me what I want.

I want this:

<Thingie>
  <someValue>blah</someValue>
  <hellaItems>
    <Item>
      <anotherValue>yaddayadda</anotherValue>
    </Item>
    <Item>
      <anotherValue>different from the first</anotherValue>
    </Item>
  </hellaItems>

</Thingie> 

I don't even know where to start...

@Stefan what if I want to do it dynamically? I don't think I understand "builders" in general may be the issue.

def items = ["yaddayadda","different from the first"]

Update: looks like im getting close, but can someone help me with this last part. I am doing this:

def items = ["yaddayadda","different from the first"]
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.thingie() {
  someValue('blah')
  hellaItems(){
      items.each{
          item(){
              anotherValue(it)
           }
      }


  }
}
def xmlString = writer.toString()
println "maybe this will just work"
println xmlString

prints:

maybe this will just work
<thingie>
  <someValue>blah</someValue>
  <hellaItems>
    <item>
      <anotherValue />
    </item>
    <item>
      <anotherValue />
    </item>
  </hellaItems>
</thingie>

Why aren't my anotherValue's there?

UPDATE: SOLVED using "tmpHolder" below, however Bill has a better syntax suggestion.

def items = ["yaddayadda","different from the first"]
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.thingie() {
  someValue('blah')
  hellaItems(){
      items.each{
          def tmpHolder = it
          item(){
              anotherValue(tmpHolder)
           }
      }


  }
}
def xmlString = writer.toString()
println "maybe this will just work"
println xmlString

解决方案

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.thingie() {
  someValue('blah')
  hellaItems(){
     item(){
        anotherValue('yaddayadda')
     }
     item(){
        anotherValue('different from the first')
     }
  }
}

writer.toString()

What don't you get? The syntax is a little weird, but that's because it's a DSL. It's not supposed to look like normal groovy "code". as XML works quite differently, and unless your object graph matches the XML you posted exactly, you won't get the results you want.

这篇关于用grails制作XML是一个好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆