我可以只向 groovy 的标记构建器提供一个节点列表吗?

发布于 2024-08-06 02:47:14 字数 524 浏览 11 评论 0原文

我正在使用 groovy 进行一些 XML 处理。具体来说,我通过 XMLParser 吸入 XML 文件,进行一整批内存中处理,然后通过 MarkupBuiler 将结果数据序列化回 XML。

文件中的绝大多数数据都会传输到非基于 xml 的对象层次结构,以与 gui 通信并完成处理,因此这两个 XML 文件永远不会相互了解。

然而,有一个 XML 片段位于源文件的边缘,需要将其逐字复制到输出 XML 文件,而无需进行任何处理。有没有办法可以将 XMLParser 中的节点列表交给 MarkupBuilder 并让它将其插入到文档中?

像这样的东西:

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

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

  }
}

I'm doing some XML processing with groovy. 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.

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.

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. Is there a way I can hand the MarkupBuilder a nodelist from XMLParser and have it just insert that into the document?

Something like this:

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

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

  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖风昔人 2024-08-13 02:47:14

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

  }
}

对于此类工作,我倾向于将 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

        }
    }
}

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.

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:

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

        }
    }
}
空袭的梦i 2024-08-13 02:47:14

以下是我如何在使用 DOMBuilder 时在中间插入 DOM 节点列表。

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

Here is how I could insert a DOM nodelist in the middle while using a DOMBuilder.

def nodelist = getFromSomewhereElse()
builder.outermosttag( name: 'library') {
  someothertag( name: 'shelf' ) {
    nodelist.each{ delegate.current.appendChild(it) }
  }
}
陈独秀 2024-08-13 02:47:14

我实际上还没有尝试过这个......但是如果你将节点列表序列化为一个字符串,你也许可以做到这一点:

builder.yieldUnescaped(xmlString)

不过仍然有点混乱......

I haven't actually tried this... but if you serialize the nodelist to a string you might be able to do this:

builder.yieldUnescaped(xmlString)

still kinda messy though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文