“如何在Mule3 anypoint studio中对xml数据进行分组”

时间:2019-05-06 17:14:44

标签: mule mule-studio anypoint-studio mulesoft

我正在执行从json到xml的转换,然后必须按部门分组。 我正在使用mysql数据库来获取员工及其部门。在最后一个变压器上,我必须按部门分组。

这是我在转换后从数据库中获取的json。

[
    {
        "id": 1,
        "Full Name": "Devendra",
        "Department": {
            "DepartmentName": "IT"
        }
    },
    {
        "id": 2,
        "Full Name": "Rohit",
        "Department": {
            "DepartmentName": "IT"
        }
    }
]

然后,我正在执行json到xml的转换,并得到以下结果。

<?xml version="1.0" encoding="windows-1252"?>
<employees>
    <employee>
        <id>1</id>
        <FullName>Devendra</FullName>
        <Department>
            <DepartmentName>IT</DepartmentName>
        </Department>
    </employee>
    <employee>
        <id>2</id>
        <FullName>Rohit</FullName>
        <Department>
            <DepartmentName>IT</DepartmentName>
        </Department>
    </employee>
</employees>

预期结果: 我需要像下面这样动态地按部门显示结果组-

<?xml version="1.0" encoding="windows-1252"?>
<Departments>
    <Department>
        <DepartmentName>IT</DepartmentName>
        <employee>
            <id>1</id>
            <FullName>Devendra</FullName>
        </employee>
        <employee>
            <id>2</id>
            <FullName>Rohit</FullName>
        </employee>
    </Department>
</Departments>

如何进行此转换?

1 个答案:

答案 0 :(得分:1)

json2xml:

%dw 1.0
%output application/xml
---
Departments: payload groupBy $.Department.DepartmentName mapObject {
    Department: 
        {DepartmentName: $$} ++
        {($ map {
            employee: {
                id: $.id,
                FullName: $.'Full Name'
            }
        })
    }
}

xml2xml:

%dw 1.0
%output application/xml
---
Departments: payload.employees groupBy $.Department.DepartmentName mapObject {
    Department: 
        {DepartmentName: $$} ++
        {($ map {
            employee: {
                id: $.id,
                FullName: $.FullName
            }
        })
    }
}