soap - php用ns解析soap响应
问题描述
我有以下肥皂反应
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns0:READResponse xmlns:ns0="/PELTT01">
<ns0:st_flag>0</ns0:st_flag>
<ns0:st_title></ns0:st_title>
<ns0:pod_date></ns0:pod_date>
<ns0:pod_time></ns0:pod_time>
<ns0:pod_name></ns0:pod_name>
<ns0:web_status>
<ns0:web_date>date1</ns0:web_date>
<ns0:web_time>time1</ns0:web_time>
<ns0:web_station>station1</ns0:web_station>
<ns0:web_status_title>title1</ns0:web_status_title>
</ns0:web_status>
<ns0:web_status>
<ns0:web_date>date2</ns0:web_date>
<ns0:web_time>time2</ns0:web_time>
<ns0:web_station>station2</ns0:web_station>
<ns0:web_status_title>title2</ns0:web_status_title>
</ns0:web_status>
...
...
<ns0:web_status_counter>5</ns0:web_status_counter>
</ns0:READResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我试图获取 web_status 节点,但一切都失败了
试试 1
$dom = new DOMDocument();
$dom->load($xml_response);
$web_status = $dom->getElementsByTagName('web_status');
试试 2
$dom = new DOMDocument();
$dom->load($xml_response);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("ns0", "/PELTT01");
$web_status = $xpath->query('//ns0:web_status');
谁能帮我解析这个肥皂响应
任何帮助表示赞赏
解决方案
如果我对您的理解正确,您正在寻找与 Try 2 接近的东西:
$dom->loadXML($xml_response);#note: NOT just "load"
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("ns0", "/PELTT01");
$web_status = $xpath->query('//ns0:web_status');
foreach ($web_status as $wb)
{
$cn = $wb->childNodes;
foreach ($cn as $c){
echo trim($c->textContent);
}
}
输出:
date1
time1
station1
title1
date2
time2
station2
title2
推荐阅读
- excel - 使用 DataFrames 每天将数据记录到 Excel 中
- python - 期望从视图返回一个 `Response`、`HttpResponse` 或 `HttpStreamingResponse`,但收到了一个`
` - ruby-on-rails - 如何使用 Ruby 将 csv 文件的内容写入电子邮件?
- javascript - 如何在 webdriverio 中编写自定义命令
- jupyter-notebook - 如何在 Jupyter 笔记本中运行命令?
- c++ - 一种避免 pgm_read 访问闪存的方法(avr 微控制器)
- c++ - 我在使用 Code::blocks 上的编译器标志时遇到问题。编程初学者
- arrays - VBA,有没有办法检查一个值是否等于数组中的任何值?
- javascript - 有没有办法处理css、html中的宽度和高度问题?
- javascript - 如何从响应体中获取深度嵌套的数据进入状态