英文:
Remove empty tags from XML in Groovy
问题
抱歉打扰您,以下是代码的翻译部分:
def body = message.getBody(java.lang.String) as String;
body = body.replaceAll("<multimap:Messages xmlns:multimap="", "");
body = body.replaceAll(""http://sap.com/xi/XI/SplitAndMerge">", "");
body = body.replaceAll("</multimap:Messages>", "");
body = body.replaceAll("multimap:Message", "base")
body = body.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
def list = new XmlParser().parseText(body)
def list1 = list.'**'.findAll{it.children().size() == 0 }
list1.each{
list.remove(it)
}
希望这有助于您解决问题。如果您有任何其他问题或需要进一步的帮助,请随时提问。
英文:
Sorry for bothering with a primitive question. i have a the following xml output with the multi map format.
The empty tags and the multi map tags need to be removed from the xml.
<?xml version='1.0' encoding='UTF-8'?>
<multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
<multimap:Message1>
<root>
<budgets/>
</root>
<root>
<budgets/>
</root>
<root>
<budgets>
<id>1234</id>
<field2>4496</field2>
<field3>test</field3>
</budgets>
</root>
<root>
<budgets/>
</root>
<root>
<budgets>
</root>
</multimap:Message1>
</multimap:Messages>
The expected output as follows
<base1>
<root>
<budgets>
<id>1234</id>
<field2>4496</field2>
<field3>test</field3>
</budgets>
</root>
</base1>
tried the following code,
def body = message.getBody(java.lang.String) as String;
body = body.replaceAll("<multimap:Messages xmlns:multimap=", "");
body = body.replaceAll("\"http://sap.com/xi/XI/SplitAndMerge\">", "");
body = body.replaceAll("</multimap:Messages>", "");
body = body.replaceAll("multimap:Message", "base")
body = body.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
def list = new XmlParser().parseText(body)
def list1 = list.'**'.findAll{it.children().size() == 0 }
list1.each{
list.remove(it)
}
Tried with the following code as well:
list.'**'.removeAll{ it.children().size() == 0 }
Not sure what i am missing here. Your help in solving this is highly appreciated.
答案1
得分: 1
使用一些简单的XML处理:
import groovy.xml.*
def xml = new XmlSlurper().parseText '''
<?xml version="1.0" encoding="UTF-8"?>
<multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
<multimap:Message1>
<root>
<budgets/>
</root>
<root>
<budgets/>
</root>
<root>
<budgets>
<id>1234</id>
<field2>4496</field2>
<field3>test</field3>
</budgets>
</root>
<root>
<budgets/>
</root>
<root>
<budgets/>
</root>
</multimap:Message1>
</multimap:Messages>'''
列表notEmptyBudgets = xml.'**'.findAll{ 'budgets' == it.name() && it.children().size() }
def writer = new StringWriter()
def out = new StreamingMarkupBuilder().bind{
base1{
root{
notEmptyBudgets.each{ b ->
budgets{
b.children().each{ "${it.name()}" it.text() }
}
}
}
}
}
XmlUtil.serialize out
返回:
```xml
<?xml version="1.0" encoding="UTF-8"?><base1>
<root>
<budgets>
<id>1234</id>
<field2>4496</field2>
<field3>test</field3>
</budgets>
</root>
</base1>
英文:
Using some simple XML processing:
import groovy.xml.*
def xml = new XmlSlurper().parseText '''\
<?xml version='1.0' encoding='UTF-8'?>
<multimap:Messages xmlns:multimap="http://sap.com/xi/XI/SplitAndMerge">
<multimap:Message1>
<root>
<budgets/>
</root>
<root>
<budgets/>
</root>
<root>
<budgets>
<id>1234</id>
<field2>4496</field2>
<field3>test</field3>
</budgets>
</root>
<root>
<budgets/>
</root>
<root>
<budgets/>
</root>
</multimap:Message1>
</multimap:Messages>'''
List notEmptyBudgets = xml.'**'.findAll{ 'budgets' == it.name() && it.children().size() }
def writer = new StringWriter()
def out = new StreamingMarkupBuilder().bind{
base1{
root{
notEmptyBudgets.each{ b ->
budgets{
b.children().each{ "${it.name()}" it.text() }
}
}
}
}
}
XmlUtil.serialize out
returns
<?xml version="1.0" encoding="UTF-8"?><base1>
<root>
<budgets>
<id>1234</id>
<field2>4496</field2>
<field3>test</field3>
</budgets>
</root>
</base1>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论