org. xml .sax.SAXParseException Префикс "a" для элемента "a: IDType" не связан в скрипте groovy - PullRequest
0 голосов
/ 04 февраля 2020

Я пытаюсь заменить какой-то узел в моем существующем конверте soap, используя скрипт groovy в инструменте SOAPUI. Ниже мой конверт exting

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
        <o:Security s:mustUnderstand="0" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2019-11-28T08:18:33.396Z</u:Created>
                <u:Expires>2019-11-28T08:23:33.396Z</u:Expires>
            </u:Timestamp>
        </o:Security>
    </s:Header>
    <s:Body>
        <ViewUserResponse xmlns="urn:myapp-com:Security.2014.pop.service.operation1">
            <ViewUserResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <LoginID xmlns:a="urn:myapp-com:Security.2012.pop.service.operation2">

                    <a:IDType>
                        <a:ID>pop</a:ID>
                        <a:Type>UserName</a:Type>
                    </a:IDType>
                    <a:IDType>
                        <a:ID>pop@we.com</a:ID>
                        <a:Type>email</a:Type>
                    </a:IDType>
                </LoginID>
                <Address xmlns:a="urn:myapp-com:Security.2012.pop.service.operation2">
                    <a:City>MH</a:City>
                    <a:State>IND</a:State>
                    <a:Pincode>1234</a:Pincode>
                </Address>
                  .
                  .
                  .

Я пытаюсь заменить часть LoginID новым контентом новый контент

                    <a:IDType>
                        <a:ID>pop_new</a:ID>
                        <a:Type>UserName</a:Type>
                    </a:IDType>
                    <a:IDType>
                        <a:ID>pop_new@we.com</a:ID>
                        <a:Type>email</a:Type>
                    </a:IDType>

Ниже мой код

def g = new XmlSlurper(false,false).parseText(newXml).declareNamespace(a:'urn:myapp-com:Security.2012.pop.service.operation2');

    existingXml.LoginID.replaceBody(g)

Это выдает ошибку org. xml .sax.SAXParseException; номер строки: 2; номер столбца: 137; Префикс «a» для элемента «a: IDType» не связан.

1 Ответ

0 голосов
/ 05 февраля 2020

Не могу понять, почему у вас ошибка, потому что вы не передаете полный код.

Но вы можете установить значения тегов, не используя replaceBody

def xml = new XmlParser().parseText('''
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Body>
        <ViewUserResponse xmlns="urn:myapp-com:Security.2014.pop.service.operation1">
            <ViewUserResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <LoginID xmlns:a="urn:myapp-com:Security.2012.pop.service.operation2">
                    <a:IDType>
                        <a:ID>pop</a:ID>
                        <a:Type>UserName</a:Type>
                    </a:IDType>
                    <a:IDType>
                        <a:ID>pop@we.com</a:ID>
                        <a:Type>email</a:Type>
                    </a:IDType>
                </LoginID>
            </ViewUserResult>
        </ViewUserResponse>
    </s:Body>
</s:Envelope>
''')

xml.'*:Body'.'*:ViewUserResponse'.'*:ViewUserResult'.'*:LoginID'.'*'.find{it.'*:Type'.text()=='UserName'}.'*:ID'[0].value='rock'
xml.'*:Body'.'*:ViewUserResponse'.'*:ViewUserResult'.'*:LoginID'.'*'.find{it.'*:Type'.text()=='email'}.'*:ID'[0].value='rock@we.com'

groovy.xml.XmlUtil.serialize(xml)

возвращает

...
        <LoginID>
          <a:IDType xmlns:a="urn:myapp-com:Security.2012.pop.service.operation2">
            <a:ID>rock</a:ID>
            <a:Type>UserName</a:Type>
          </a:IDType>
          <a:IDType xmlns:a="urn:myapp-com:Security.2012.pop.service.operation2">
            <a:ID>rock@we.com</a:ID>
            <a:Type>email</a:Type>
          </a:IDType>
        </LoginID>
...
...