문제

I'm calling a web service like this (using rest plugin):

withRest(uri: "http://server.com") {
    def response = post(path: '/webservice', query: [q: 'test'])
    // process response
}

and getting the following response:

<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
    <MULTIPLE>
        <SINGLE>
            <KEY name="id">
                <VALUE>1</VALUE>
            </KEY>
            <KEY name="courseid">
                <VALUE>1</VALUE>
            </KEY>
            <KEY name="name">
                <VALUE>test</VALUE>
            </KEY>
            <KEY name="description">
                <VALUE>Test</VALUE>
            </KEY>
            <KEY name="descriptionformat">
                <VALUE>1</VALUE>
            </KEY>
            <KEY name="enrolmentkey">
                <VALUE>TEST</VALUE>
            </KEY>
        </SINGLE>
    </MULTIPLE>
</RESPONSE>

I'd like to convert this response to an object so that I can do something like this:

assert responseMap.id == 1

The only way I know how to do this is use Gpath expressions in the // process request block above to grab individual entries and build my responseMap. Is there any easier way? Does this response follow some 'standard' and are there functions to make my job easier?

도움이 되었습니까?

해결책

You can just do:

new XmlSlurper().parseText( response )
                .MULTIPLE.SINGLE.KEY
                .find { it.@name == 'id' }
                .VALUE.text() == '1'

To check the VALUE of the KEY tag with name="id" is 1

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top