Groovy XmlSlurper解析错误 - prolog中不允许使用内容

时间:2018-02-21 14:47:19

标签: xml groovy prolog jmeter-3.2

我正在尝试解析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>

关于我做错了什么的想法

2 个答案:

答案 0 :(得分:1)

你的错误变量与行中的值:

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

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

def respParse = parse.parseText(xmlContent);

答案 1 :(得分:0)

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

      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());