Я пытаюсь написать функцию моста для преобразования XML-данных в формат Json. Ниже приведены данные, которые у меня есть. Пример XML-файла:
<testsuites> <testsuite tests="4" failures="4" errors="0" name="AT">
<testcase name="#1 notificate › v1 › announcement › announcement.feature/#TEST CASE: Notification: Send an announcement: Send an announcement using the minimum requirements"/>
<testcase name="#2 notifiivate › v1 › announcement › announcement.feature/#TEST CASE: Notification: Send an ant"/>
<testcase name="#1 No tests found in features/tests/auth/auth.POST.js">
<failure/>
</testcase>
<testcase name="#2 versioninfo › versioninfo › versioninfo.feature/#TEST CASE: CDP ADMIN: Get version info: Get the version of the CDP service">
<failure>
name: AssertionError
message: Rejected promise returned by test
values:
</failure>
</testcase>
<testcase name="#3 projects › edit_entitlement › edit_entitlement.feature/#TEST CASE: CDP ADMIN: Edit Entitlement: Attempt to edit an entitlement_id to be a negative number">
<failure>
---
name: AssertionError
message: Rejected promise returned by test
values:
...
</failure>
</testcase>
</testsuite>
</testsuites>
Я пытаюсь написать функцию в groovy, чтобы получитьприведенный ниже формат json
{
testsuites{
"testsuite": {
"tests": "4",
"failures": "4",
"errors": "0",
"name": "AT-cdpServer.Default",
"testcase": [
{
"name": "#1 notificate › v1 › announcement › Send an announcement: Send an announcement using the minimum requirements"
},
{
"name": "#2 notifiivate › v1 › announcement › announcement.feature/#TEST CASE: Notification: Send an ant"
},
{
"name": "#1 No tests found in features/tests/auth/auth.POST.js",
"failure": []
},
{
"name": "#2 versioninfo › versioninfo › versioninfo.feature/#TEST CASE: CDP ADMIN: Get version info: Get the version of the CDP service",
"failure": "---\n name: AssertionError\n message: Rejected promise returned by test\n values: {\"Rejected promise returned by test. Reason:\":\"Error {\\n message: 'no schema with key or ref \\\"/versioninfo.get.200\\\"',\\n}\"}\n at: Ajv.validate (node_modules/ajv/lib/ajv.js:95:19)\n ..."
},
{
"name": "#3 projects › edit_entitlement › edit_entitlement.feature/#TEST CASE: CDP ADMIN: Edit Entitlement: Attempt to edit an entitlement_id to be a negative number",
"failure": "---\n name: AssertionError\n message: Rejected promise returned by test\n values: {\"Rejected promise returned by test. Reason:\":\"TypeError {\\n message: 'Only absolute URLs are supported',\\n}\"}\n ..."
},
]
}
}}
}
Оцените любые входные данные в правильном направлении, спасибо
Пока у меня есть это, он читает все данные, но структура отключена
def toJsonBuilder(xml){
def xmlToJson = build(new XmlSlurper().parseText(xml))
new groovy.json.JsonBuilder(xmlToJson)
}
def build(node){
if (node instanceof String){
return // ignore strings...
}
def map = [(node.name()): node.collect]
if (!node.attributes().isEmpty()) {
map.put(node.name(),node.attributes().collectEntries{it})
}
if (!node.children().isEmpty() && !(node.children().getAt(0) instanceof String)) {
map.put(node.children().name, node.children().collect{build(it)}.findAll{it != null})
} else if (node.text() != ''){
map.put(node.name(), node.text())
}
map
}