在Groovy中使用XML MarkupBuilder动态添加多个XML元素/容器

在Groovy中使用XML MarkupBuilder动态添加多个XML元素/容器,xml,groovy,markupbuilder,Xml,Groovy,Markupbuilder,我正在尝试使用Groovy MarkupBuilder生成XML 所需的XML格式如下(简化): “项目”内可以有多个“项目”容器 我的问题是: 假设我们要生成包含10项的订单XML。有没有办法在“items”容器中编写类似for循环的内容?这样,我们就不需要为10个不同的项目编写MarkupBuilder代码 还有一个类似的问题。但是它没有讨论循环。是的,有一种使用循环的方法。在此扩展您的示例: import groovy.xml.* def writer = new StringWriter

我正在尝试使用Groovy MarkupBuilder生成XML

所需的XML格式如下(简化):

“项目”内可以有多个“项目”容器

我的问题是: 假设我们要生成包含10项的订单XML。有没有办法在“items”容器中编写类似for循环的内容?这样,我们就不需要为10个不同的项目编写MarkupBuilder代码


还有一个类似的问题。但是它没有讨论循环。

是的,有一种使用循环的方法。在此扩展您的示例:

import groovy.xml.*
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

//List of items represented as a map
def items = [[itemCode: "A", unitPrice: 10, quantity: 2], 
             [itemCode: "B", unitPrice: 20, quantity: 3], 
             [itemCode: "C", unitPrice: 30, quantity: 4], 
             [itemCode: "D", unitPrice: 40, quantity: 6], 
             [itemCode: "E", unitPrice: 50, quantity: 5]]

xml.Order{
  StoreID("Store1")
  City("New York")
  Items{
    //Loop through the list.
    //make sure you are using a variable name instead of using "it"
    items.each{item->
      Item{
        ItemCode(item.itemCode)
        UnitPrice(item.unitPrice)
        Quantity(item.quantity)
      }
    }
  }
}

println writer

这一贡献帮助我解决了一个类似的问题:我想调用MarkupBuilder块中的函数,比如示例中的
addElement()
函数

我想把代码分成不同的函数

在MarkupBuilder块中调用函数的示例:

static void addElement(Map<String,String> elements, MarkupBuilder mb) {
    mb."${elements.tag}"(elements.content)
}

static void example() {
    def writer = new StringWriter()
    def htmlBuilder = new MarkupBuilder(writer)

    String tag = "a"
    Map<String, String> attributes1 = new HashMap<>()
    attributes1.put("href","http://address")
    String content1 = "this is a link"

    Map<String, String> element1 = new HashMap<>()
    element1.put("tag","b")
    element1.put("content","bold content 1")

    Map<String, String> element2 = new HashMap<>()
    element2.put("tag","b")
    element2.put("content","bold content 2")

    List<Map<String, String>> elements = new ArrayList<>()
    elements.add(element1)
    elements.add(element2)

    htmlBuilder."html" {
        "${tag}"( attributes1, content1 )
        elements.each { contentIterator ->
            addElement(contentIterator, htmlBuilder)
        }
    }

    println writer
 }
static void addElement(映射元素,MarkupBuilder mb){
mb.“${elements.tag}”(elements.content)
}
静态void示例(){
def writer=new StringWriter()
def htmlBuilder=新标记生成器(编写器)
String tag=“a”
Map attributes1=新的HashMap()
属性1.put(“href”http://address")
String content1=“这是一个链接”
Map element1=新的HashMap()
要素1.放置(“标签”、“b”)
要素1.put(“内容”、“粗体内容1”)
Map element2=新的HashMap()
元素2.放置(“标签”、“b”)
要素2.put(“内容”、“粗体内容2”)
列表元素=新的ArrayList()
元素。添加(元素1)
元素。添加(元素2)
htmlBuilder.“html”{
“${tag}”(属性1,内容1)
elements.each{content迭代器->
addElement(contentIterator、htmlBuilder)
}
}
印刷机
}
它产生这个输出:

<html>
  <a href='http://address'>this is a link</a>
  <b>bold content 1</b>
  <b>bold content 2</b>
</html>

粗体内容1
粗体内容2

我明白了。因此,我们可以在标记内编写常规Groovy循环代码。我认为标记应该只包含标记代码。谢谢你的详细回答。谢谢!:)谢谢你的建议。这帮了我很大的忙。谢谢你的回答!作为一个Groovy新手,我搜索了很多,直到找到这个!
static void addElement(Map<String,String> elements, MarkupBuilder mb) {
    mb."${elements.tag}"(elements.content)
}

static void example() {
    def writer = new StringWriter()
    def htmlBuilder = new MarkupBuilder(writer)

    String tag = "a"
    Map<String, String> attributes1 = new HashMap<>()
    attributes1.put("href","http://address")
    String content1 = "this is a link"

    Map<String, String> element1 = new HashMap<>()
    element1.put("tag","b")
    element1.put("content","bold content 1")

    Map<String, String> element2 = new HashMap<>()
    element2.put("tag","b")
    element2.put("content","bold content 2")

    List<Map<String, String>> elements = new ArrayList<>()
    elements.add(element1)
    elements.add(element2)

    htmlBuilder."html" {
        "${tag}"( attributes1, content1 )
        elements.each { contentIterator ->
            addElement(contentIterator, htmlBuilder)
        }
    }

    println writer
 }
<html>
  <a href='http://address'>this is a link</a>
  <b>bold content 1</b>
  <b>bold content 2</b>
</html>