如何在Groovy脚本中的平面文件中连接列表的元素

k4aesqcs  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(61)

我试图从下面的xml中获取所有值,并将其分割为一个字符串,|“

<n0:SendData xmlns:n0="http://testdata/"
                 xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <Operation>Testing1</Operation>
    <InputDataList>
        <InputData>
            <Field>ATTRIBUTE1</Field>
            <Value>1.000</Value>
        </InputData>
        <InputData>
            <Field>ATTRIBUTE2</Field>
            <Value>test</Value>
        </InputData>
        <InputData>
            <Field>ATTRIBUTE3</Field>
        </InputData>
        <InputData>
            <Field>ATTRIBUTE4</Field>
            <Value>testend</Value>
        </InputData>
    </InputDataList>
    <InputDataList>
        <InputData>
            <Field>ATTRIBUTE1</Field>
            <Value>2.000</Value>
        </InputData>
        <InputData>
            <Field>ATTRIBUTE2</Field>
            <Value>test2</Value>
        </InputData>
        <InputData>
            <Field>ATTRIBUTE3</Field>
            <Value>test2end</Value>
        </InputData>
        <InputData>
            <Field>ATTRIBUTE4</Field>
        </InputData>
    </InputDataList>
</n0:SendData>

字符串
我想出来的代码:

def xml = new XmlSlurper().parseText(data)

def items = xml.InputDataList.each { node ->
    node.InputData.Field.text() == ""
}.collect { node ->
    node.InputData.with {
        "${InputData.text()}|\n"
    }
}

println items


这将导致以下响应
1.000testtestend|
2.000test2testend2|
但我找不到方法添加“|“在列表的每个元素之后,即使值为空?
我正在寻找的结果如下
1.000|测试||试验的
2.000| test2| test2end||

bxpogfeg

bxpogfeg1#

你想collectValue s为每个InputDataListjoin他们与|

def items = xml.InputDataList.collect { node ->
        node.InputData*.Value.join("|")
}.join("\n")

字符串
在你最初的尝试中,each是一个无效操作,因为那里的比较只会加热你的房间。你想用findAll代替吗?

相关问题