【问题标题】:org.xml.sax.SAXParseException The prefix "a" for element "a:IDType" is not bound in groovy scriptorg.xml.sax.SAXParseException 元素“a:IDType”的前缀“a”未绑定在 groovy 脚本中
【发布时间】:2020-02-04 06:41:14
【问题描述】:

我正在尝试使用 SOAPUI 工具中的 groovy 脚本替换我现有的肥皂信封中的一些节点 下面是我的包装盒

<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:IDType”的前缀“a”未绑定。

【问题讨论】:

    标签: groovy soapui xmlslurper


    【解决方案1】:

    我猜不出您为什么会出现错误,因为您没有共享完整的代码。

    但是你可以设置标签的值而不使用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>
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 2013-01-14
      • 2015-01-29
      • 2011-08-10
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      相关资源
      最近更新 更多