获取具有名称和值Groovy的所有XML ChildNodes

时间:2016-08-17 07:50:16

标签: xml groovy

我需要获取子项和子项以及标记名并将其写入XML。是否有任何方法在groovy中做到这一点。这是我的输入XML

def employees='''<Employees>
                      <Employee>
                         <ID>123</ID>
                         <Name>xyz</Name>
                         <Addresses>
                             <Address>
                                <Country>USA</Country>
                                <ZipCode>40640</ZipCode>
                             </Address>
                          </Addresses>
                      </Employee>
                      <Employee>
                         <ID>345</ID>
                         <Name>abc</Name>
                         <Addresses>
                             <Address>
                                <Country>CA</Country>
                                <ZipCode>50640</ZipCode>
                             </Address>
                          </Addresses>
                      </Employee>
                    </Employees>'''

def lst=["ID","Addresses"]

def sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
    def response = new XmlSlurper().parseText(employees)
    xml.Employee{response.Employee.'**'.findAll{ node->        
           if(node.name() == str)
           {
           if(node.children().size()>0)
           {
          //get the children and subchildren of Addresses directly and write to output      
           }
           else
           {
          //get the element name and value which is not having children
            }
    }
}
}

我的预期输出应为

                    <Employees>
                      <Employee>
                         <ID>123</ID>
                         <Addresses>
                             <Address>
                                <Country>USA</Country>
                                <ZipCode>40640</ZipCode>
                             </Address>
                          </Addresses>
                      </Employee>
                        <Employee>
                         <ID>345</ID>
                         <Addresses>
                             <Address>
                                <Country>CA</Country>
                                <ZipCode>50640</ZipCode>
                             </Address>
                          </Addresses>
                      </Employee>                        
                    </Employees>

由于ID和地址出现在列表中

0 个答案:

没有答案