Groovy XML Holder 为未格式化的 xml 返回 null



我正在使用xmlholder检索xml标签,但它不适用于未格式化的xml。

def holder = groovyUtils.getXmlHolder(mockRequest.requestContent);
holder.declareNamespace("ns2", "http://example.com")
if(holder.getNodeValue('//ns2:GetCustomerInfo')!=null){
println true
}

我对格式化的 xml 越来越真实:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
   <S:Header>
   </S:Header>
   <S:Body>
      <ns2:GetCustomerInfo xmlns:ns2="http://example.com">
         <ns2:Identifier>4111119876543210</ns2:Identifier>
      </ns2:GetCustomerInfo>
   </S:Body>
</S:Envelope>

如果 xml 未格式化并作为一行字符串给出,我就不会得到真。

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header></S:Header><S:Body><ns2:GetCustomerInfo xmlns:ns2="http://example.com"><ns2:Identifier>4111119876543210</ns2:Identifier></ns2:GetCustomerInfo></S:Body></S:Envelope>

实际上,我需要从未格式化的xml中检索值,因为我将获取未格式化的数据。

您的代码按预期工作!

  1. 在,你所说的,"格式化大小写"节点的值 GetCustomerInfo是一个换行符和一些空格,所以它不null
  2. 在"未格式化的情况下",节点的值 GetCustomerInfo什么都不是,所以它null.

为了证明这一点,您可以在 GetCustomerInfo 节点之后的无格式字符串中插入单个空格字符,然后再次运行测试。

您可能想尝试使用 getDomNode() 而不是 getNodeValue() 来获得您正在寻找的行为。

最新更新