Groovy XmlSlurper 解析错误 - prolog 中不允许包含内容

Groovy XmlSlurper parse error - content is not allowed in prolog

我正在尝试解析 jmeter soap 响应 (XML),但出现解析错误

org.xml.sax.SAXParseException: Content is not allowed in prolog

我已使用在线工具验证 SOAP 响应 (XML) 有效,但无济于事。我仍然得到一个错误。即使我尝试获得漂亮的 XML 输出,我也会收到 null 错误,因为解析结果为 null.

代码片段如下:

import groovy.util.XmlSlurper;
import groovy.util.XmlParser;
import groovy.util.slurpersupport.NodeChild;

def xmlContent = ctx.getPreviousResult().getResponseDataAsString();
log.info(xmlContent);

def stringWriter = new StringWriter()
def parse = new XmlSlurper();
def respParse = parse.parseText(xmlContent);
log.info(respParse);
def test = new XmlNodePrinter(new PrintWriter(stringWriter)).print(respParse) 
log.info(test);

XML 是:

<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <ns2:Response xmlns:ns2="URL" xmlns="URL" xmlns:ns4="URL" xmlns:ns3="URL" xmlns:ns5="URL">
                 <something>Code</something>
                 <somethingDescription>Message</somethingDescription>
             <ns2:pending/>
                 <ns2:posted/>
         </ns2:Response>
     </soapenv:Body>
</soapenv:Envelope>

关于我做错了什么的任何想法

您误将行中的值设为变量:

def respParse = parse.parseText("xmlContent");

您传递的是字符串而不是变量 xmlContent。应该是:

def respParse = parse.parseText(xmlContent);

我设法用以下代码解决了这个问题:

      import groovy.util.XmlSlurper;



       def xmlContent = ctx.getPreviousResult().getResponseDataAsString();
       log.info(xmlContent);

       def resp = new XmlSlurper().parseText(xmlContent);

       def result = resp.Body.Response;
       def respCode = resp.Body.Response.ResponseCode;
       def respCodeDesc = resp.Body.Response.Description;
       log.info(respCode.toString());