简体   繁体   English

我可以将groovy的markupbuilder交给节点列表吗?

[英]Can I just hand groovy's markupbuilder a node list?

I'm doing some XML processing with groovy. 我正在使用groovy进行一些XML处理。 Specifically, I'm inhaling an XML file via XMLParser, doing a whole batch of in-memory processing, and then serializing the resulting data back out to XML via a MarkupBuiler. 具体来说,我通过XMLParser吸入XML文件,进行一整堆内存处理,然后通过MarkupBuiler将结果数据序列化回XML。

The vast majority of the data in the file gets transferred to a non-xml based object hierarchy to talk to the gui and have processing done, so the two XML files never know about each other. 文件中的绝大多数数据都被传输到非基于xml的对象层次结构以与gui通信并完成处理,因此两个XML文件永远不会彼此了解。

However, there is one slug of XML that lives at the edge of the source file that needs to get copied verbatim to the output XML file with little or no processing. 但是,有一个XML版本位于源文件的边缘,需要逐个复制到输出XML文件,几乎不需要处理。 Is there a way I can hand the MarkupBuilder a nodelist from XMLParser and have it just insert that into the document? 有没有办法让MarkupBuilder从XMLParser中获取一个nodelist并将其插入到文档中?

Something like this: 像这样的东西:

def builder = new MarkupBuilder()
builder.outermosttag( name: 'library') {
  someothertag( name: 'shelf' ) {

    //magically insert a nodelist of arbitrary XML from somewhere else

  }
}

XmlParser returns a Node and I don't know of any way to insert it into a Markup or StreamingMarkupBuilder without converting to a xml string. XmlParser返回一个Node,我不知道如何将它插入Markup或StreamingMarkupBuilder而不转换为xml字符串。

import groovy.xml.*

def x = """
<root>
  <somenode1>
      <anode>foo</anode>
  </somenode1>
  <somenode2>
      <anode>bar</anode>
  </somenode2>
</root>
""".trim()
def otherXml = new XmlParser().parseText(x)

def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(otherXml.somenode1[0])

def builder = new MarkupBuilder()
builder.outermosttag( name: 'library') {
  someothertag( name: 'shelf' ) {

    mkp.yieldUnescaped writer.toString() //magically insert a nodelist of arbitrary XML from somewhere else

  }
}

For this kind of work I tend to use XmlSlurper along with the StreamingMarkupBuilder: 对于这种工作,我倾向于使用XmlSlurper和StreamingMarkupBuilder:

import groovy.xml.*

def x = """
<root>
  <somenode1>
      <anode>foo</anode>
  </somenode1>
  <somenode2>
      <anode>bar</anode>
  </somenode2>
</root>
""".trim()
def otherXml = new XmlSlurper().parseText(x)

def builder = new StreamingMarkupBuilder().bind {
    outermosttag( name: 'library') {
        someothertag( name: 'shelf' ) {

            mkp.yield otherXml.somenode1 //magically insert a nodelist of arbitrary XML from somewhere else

        }
    }
}

Here is how I could insert a DOM nodelist in the middle while using a DOMBuilder . 这是我在使用DOMBuilder在中间插入DOM节点列表的方法。

def nodelist = getFromSomewhereElse()
builder.outermosttag( name: 'library') {
  someothertag( name: 'shelf' ) {
    nodelist.each{ delegate.current.appendChild(it) }
  }
}

I haven't actually tried this... but if you serialize the nodelist to a string you might be able to do this: 我还没有尝试过这个...但是如果你将nodelist序列化为一个字符串,你可能会这样做:

builder.yieldUnescaped(xmlString)

still kinda messy though... 虽然有点凌乱......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM