Groovy-合并XML节点 [英] Groovy - merging XML nodes

查看:130
本文介绍了Groovy-合并XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此非常了解,但是对我来说不起作用.我有两个XML结构

I'm so close on this but it's just not working for me. I have two XML structures

结构为项->项(1到许多实例).

The structure is Items -> Item (1 to many instances).

我有一个包含Item的现有Item,并且我想将所有实例从Item的第二个副本复制到现有副本.

I have an existing Item, with Items, and I want to copy all instances from a second copy of Item to the existing one.

def newMessage = new XmlParser().parseText(newMsg);
def newItems = newMessage.depthFirst().findAll{it.name() == 'Items'};

def existingMessage = new XmlParser().parse(src); //src is a file location
def existingSettlementItem = existingMessage.depthFirst().findAll{it.name() == 'Items'};

这给了我一个大小为2的数组列表,这是预期的.我可以将两个数组很好地组合在一起,这将为我的节点提供4大小的arrayList.如何将其返回到XML?

This gives me an array list of size 2, which is expected. I can combined the two arrays fine, which will give me a 4 size arrayList of my nodes. How can I get this back to the XML?

关于, 克里斯

添加了XML示例

<Items>
  <SItem>
    <MsgData>
      <BahElements>
        <Fr>
          <Name>
            <Address>
              <Value>123 Fake St</Value>
            </Address>
          </Name>
        </Fr>
        <To>
         <Name>
            <Address>
              <Value>123 Fake St</Value>
            </Address>
          </Name>
        </To>
        <MsgDefIdr>MSG_DEF_IDR_VALUE</MsgDefIdr>
      </BahElements>
    </MsgData>
  </Item>
  <Item>
     <MsgData>
      <BahElements>
        <Fr>
          <Name>
            <Address>
              <Value>123 Fake St</Value>
            </Address>
          </Name>
        </Fr>
        <To>
         <Name>
            <Address>
              <Value>321 Fake St</Value>
            </Address>
          </Name>
        </To>
        <MsgDefIdr>MSG_DEF_IDR_VALUE</MsgDefIdr>
      </BahElements>
    </MsgData>
  </Items>

推荐答案

这里是groovy script,它将项目节点从两个xml数据合并为一个xml.

Here is the groovy script which does the merging of Item nodes from two xml data into one xml.

您需要做的就是从其他xml中选择要合并Item节点并附加这些节点的对象. 例如,需要将xml2的项目合并到xml1吗?然后使用:xml2Items.collect{ pXml1.Items.appendNode(it)}

All you need to is choose the object where you want merge Item nodes from other xml and append those nodes. For instance, Items of xml2 needs to be merged into xml1? Then use: xml2Items.collect{ pXml1.Items.appendNode(it)}

我相信现在您知道如何进行合并,即将xml1的项目合并到xml2中.

And I believe now you know how to do the merge the otherway i.e., Items of xml1 to merge into xml2.

//Create the parsed objects for xml data
def pXml1 = new XmlSlurper().parseText(xml1)
def pXml2 = new XmlSlurper().parseText(xml2)

//Merge the 2nd xml nodes into 1st xml
pXml2.'**'.findAll{it.name() == 'Item'}.collect{ pXml1.Items.appendNode(it)}

//Print the whole merged xml
println groovy.xml.XmlUtil.serialize(pXml1)

您可以通过xml示例快速在线上 演示 提供的数据

You can quickly try this online Demo with xml sample data provided

请注意,xml示例数据的格式不正确,因此在上面的演示中已进行了更改.

Note that the xml sample data is not well-formed, so had changed in the above demo.

这篇关于Groovy-合并XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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