从XML中删除空标签在Groovy中的实现:

huangapple go评论57阅读模式
英文:

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 &#39;&#39;&#39;\
&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
&lt;multimap:Messages xmlns:multimap=&quot;http://sap.com/xi/XI/SplitAndMerge&quot;&gt;
    &lt;multimap:Message1&gt;
        &lt;root&gt;
            &lt;budgets/&gt;
        &lt;/root&gt;
        &lt;root&gt;
            &lt;budgets/&gt;
        &lt;/root&gt;
        &lt;root&gt;
            &lt;budgets&gt;
                &lt;id&gt;1234&lt;/id&gt;
                &lt;field2&gt;4496&lt;/field2&gt;
                &lt;field3&gt;test&lt;/field3&gt;
            &lt;/budgets&gt;
        &lt;/root&gt;
        &lt;root&gt;
            &lt;budgets/&gt;
        &lt;/root&gt;
        &lt;root&gt;
            &lt;budgets/&gt;
        &lt;/root&gt;
    &lt;/multimap:Message1&gt;
&lt;/multimap:Messages&gt;&#39;&#39;&#39;

List notEmptyBudgets = xml.&#39;**&#39;.findAll{ &#39;budgets&#39; == it.name() &amp;&amp; it.children().size() }

def writer = new StringWriter()

def out = new StreamingMarkupBuilder().bind{
  base1{
    root{
      notEmptyBudgets.each{ b -&gt;
        budgets{
          b.children().each{ &quot;${it.name()}&quot; it.text() }
        }
      }
    }
  }
}

XmlUtil.serialize out

returns

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;base1&gt;
  &lt;root&gt;
    &lt;budgets&gt;
      &lt;id&gt;1234&lt;/id&gt;
      &lt;field2&gt;4496&lt;/field2&gt;
      &lt;field3&gt;test&lt;/field3&gt;
    &lt;/budgets&gt;
  &lt;/root&gt;
&lt;/base1&gt;

huangapple
  • 本文由 发表于 2023年2月10日 16:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定