php - PHP XML 不使用 SimpleXml 解析
问题描述
我的 xml 没有解析,我不知道为什么第一行 xml 没有解析,但第二行解析好
我知道我错过了代码中的任何内容,但在谷歌中搜索并没有找到正确的答案
// this xml not work, with <soap:Envelope> tags
$string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetBrandsListResponse xmlns="http://tempuri.org/">
<GetBrandsListResult>
<DocumentElement>
<BrandLst>
<ID>1</ID>
<Name>Audi</Name>
</BrandLst>
<BrandLst>
<ID>350</ID>
<Name>BMW</Name>
</BrandLst>
</DocumentElement>
</GetBrandsListResult>
</GetBrandsListResponse>
</soap:Body>
</soap:Envelope>';
// but this xml works, without soap envelope tags
$string = '
<BrandLst>
<ID>1</ID>
<Name>Audi</Name>
</BrandLst>
';
$xml = simplexml_load_string($string);
var_dump($xml);
解决方案
通过添加 xpath 和 registerXPathNamespace 修复
$xml = simplexml_load_string($string);
$xml->registerXPathNamespace('default', 'http://tempuri.org/');
$auto = $xml->xpath("//default:BrandLst");
推荐阅读
- php - Windows IIS Web 服务器上的随机 Dropzone 错误
- javascript - 在 php 中从 iframe 编辑主页变量值
- django - Django - 如何忽略自动模型语言过滤器
- javascript - 尝试在反应中使用异步声明但在构建中出错
- python - 创建一个闭源 Python 程序
- go - 如何解决 bufio.Scanner.Scan 耗尽 io.Reader
- python - pandas 通过添加新列分组并计算总数
- abap - 如何以高效的方式迭代 ABAP 中的字符串字符?
- r - 拟合一条线(渐近线?)强制通过 0
- javascript - Flutter Web 文本字段消失或花费大量时间在两个文本字段之间转换