首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium Webdriver :验证httpResponse xml

Selenium Webdriver :验证httpResponse xml
EN

Stack Overflow用户
提问于 2019-01-13 02:26:31
回答 1查看 269关注 0票数 0

我想验证从httpResponse获得的XML响应。

我的代码

代码语言:javascript
复制
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost(decouplingURL);
 httpPost.setEntity(new StringEntity(soapRequest));
 System.out.println(soapRequest);
 HttpResponse httpResponse = httpclient.execute(httpPost);
 System.out.println(httpResponse);
 String resp = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
 System.out.println(resp);
 String payload = "";
 NodeList nl = doc.getElementsByTagName("payload");
 for (int i = 0; i < nl.getLength(); i++) {
     if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
         org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
         payload = nameElement.getFirstChild().getNodeValue().trim();
     }
 }

在上面的代码中,我得到了以下响应,我想要获取@status并验证它的值是否为1。类似地,我必须验证weaher 'charge-method‘是否为3

代码语言:javascript
复制
<?xml version='1.0' encoding='UTF-8'?>
<er-response>
  <payload>
      <subscription id="33517965" status="1">
        <pricepoint id="package:aceklpackage">
          <charge-method>3</charge-method>
          <rate resource="EUR" tax-rate="0.23">30.0</rate>
          <user-group>hover32</user-group>
        </pricepoint>
      </subscription>
  </payload>
</<er-response>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-13 04:26:04

不确定这个问题与Apache有什么关系,您正在使用Apache HttpClient。您可以简单地使用XPath从xml中提取所需的元素:

代码语言:javascript
复制
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = builder.parse(new ByteArrayInputStream(resp.getBytes("UTF-8")));

XPath xPath = XPathFactory.newInstance().newXPath();

System.out.printf("Status: %s\n", xPath.evaluate("//subscription/@status", doc));
System.out.printf("Charge Method: %s\n", xPath.evaluate("//charge-method//text()", doc));

结果:

代码语言:javascript
复制
Status: 1
Charge Method: 3
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54162609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档