根据employment_information节点的出现情况创建多个ComponentEmployee节点,保持父节点不变
Input:
queryCompoundEmployeeResponse
CompoundEmployee
person
personal_information
email_information
employment_information
employment_information
CompoundEmployee
person
personal_information
email_information
employment_information
Output:
queryCompoundEmployeeResponse
CompoundEmployee
person
personal_information
email_information
employment_information
CompoundEmployee
person
personal_information
email_information
employment_information
CompoundEmployee
person
personal_information
email_information
employment_information
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
def xml = '''
<?xml version="1.0" encoding="UTF-8"?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
<id>Dummy1</id>
<person>
<person_id_external>Dummy1</person_id_external>
<personal_information>
<first_name>Test01</first_name>
<gender>M</gender>
<last_name>test01</last_name>
</personal_information>
<employment_information>
<isContingentWorker>false</isContingentWorker>
<jobNumber>1</jobNumber>
<start_date>2019-04-01</start_date>
<job_information>
<company>LE-USA-001</company>
</job_information>
</employment_information>
<employment_information>
<isContingentWorker>false</isContingentWorker>
<jobNumber>2</jobNumber>
<start_date>2019-04-01</start_date>
<job_information>
<company>LE-USA-004</company>
</job_information>
</employment_information>
</person>
</CompoundEmployee>
<CompoundEmployee>
<id>Dummy2</id>
<person>
<person_id_external>Dummy2</person_id_external>
<personal_information>
<first_name>Test02</first_name>
<gender>M</gender>
<last_name>test02</last_name>
</personal_information>
<employment_information>
<isContingentWorker>true</isContingentWorker>
<jobNumber>3</jobNumber>
<job_information>
<company>LE-BRA-001</company>
</job_information>
</employment_information>
</person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>
'''
def root = new XmlParser().parseText(xml)
def newXml = new StreamingMarkupBuilder().bind {
queryCompoundEmployeeResponse{
root.CompoundEmployee.each { compoundEmployee ->
compoundEmployee.person.employment_information.each { employmentInfo ->
CompoundEmployee{
id(compoundEmployee.id)
person{
person_id_external(compoundEmployee.person.person_id_external)
personal_information(compoundEmployee.person.personal_information)
email_information(compoundEmployee.person.email_information)
employment_information(employmentInfo)
}
}
}
}
}
}
println XmlUtil.serialize(newXml)
输出:
<queryCompoundEmployeeResponse>
<CompoundEmployee>
<id>id[attributes={}; value=[Dummy1]]</id>
<person>
<person_id_external>person_id_external[attributes={}; value=[Dummy1]]</person_id_external>
<personal_information>personal_information[attributes={}; value=[first_name[attributes={}; value=[Test01]], gender[attributes={}; value=[M]], last_name[attributes={}; value=[test01]]]]</personal_information>
<email_information>email_information[attributes={}; value=[email_address[attributes={}; value=[DUMMY1@dummy.com]], email_type[attributes={}; value=[B]], isPrimary[attributes={}; value=[true]]]]</email_information>
<employment_information>isContingentWorker[attributes={}; value=[false]]jobNumber[attributes={}; value=[1]]start_date[attributes={}; value=[2019-04-01]]job_information[attributes={}; value=[company[attributes={}; value=[LE-USA-001]], start_date[attributes={}; value=[2020-04-24]]]]job_information[attributes={}; value=[company[attributes={}; value=[LE-USA-002]], start_date[attributes={}; value=[2020-04-23]]]]</employment_information>
</person>
</CompoundEmployee>
<CompoundEmployee>
<id>id[attributes={}; value=[Dummy1]]</id>
<person>
<person_id_external>person_id_external[attributes={}; value=[Dummy1]]</person_id_external>
<personal_information>personal_information[attributes={}; value=[first_name[attributes={}; value=[Test01]], gender[attributes={}; value=[M]], last_name[attributes={}; value=[test01]]]]</personal_information>
<email_information>email_information[attributes={}; value=[email_address[attributes={}; value=[DUMMY1@dummy.com]], email_type[attributes={}; value=[B]], isPrimary[attributes={}; value=[true]]]]</email_information>
<employment_information>isContingentWorker[attributes={}; value=[false]]jobNumber[attributes={}; value=[2]]start_date[attributes={}; value=[2019-04-01]]job_information[attributes={}; value=[company[attributes={}; value=[LE-USA-004]], start_date[attributes={}; value=[2020-04-24]]]]</employment_information>
</person>
</CompoundEmployee>
<CompoundEmployee>
<id>id[attributes={}; value=[Dummy2]]</id>
<person>
<person_id_external>person_id_external[attributes={}; value=[Dummy2]]</person_id_external>
<personal_information>personal_information[attributes={}; value=[first_name[attributes={}; value=[Test02]], gender[attributes={}; value=[M]], last_name[attributes={}; value=[test02]]]]</personal_information>
<email_information>email_information[attributes={}; value=[email_address[attributes={}; value=[DUMMY2@dummy.com]], email_type[attributes={}; value=[B]], isPrimary[attributes={}; value=[true]]]]</email_information>
<employment_information>isContingentWorker[attributes={}; value=[true]]jobNumber[attributes={}; value=[3]]job_information[attributes={}; value=[company[attributes={}; value=[LE-BRA-001]], start_date[attributes={}; value=[2020-04-24]]]]</employment_information>
</person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>
问题:输出xml包含值为Node类型的节点personal_information、email_information和employment_information,需要像输入示例中那样转换为xml。
<personal_information>
personal_information[attributes={}; value=[first_name[attributes={}; value=[Test01]], gender[attributes={}; value=[M]], last_name[attributes={}; value=[test01]]]]
</personal_information>
<personal_information>
<first_name>Test01</first_name>
<gender>M</gender>
<last_name>test01</last_name>
</personal_information>
你能纠正我在这里做错了什么吗?
注意:该模式非常大,包含多个字段。我刚刚做了一个简单的模式。代码需要动态生成该特定节点的所有字段/元素。