[英]How to convert xml response string into simple xml object in php
再会。
我无法在我的 xml 响应中使用 simplexml_load_string function。 object 返回为空。
有什么线索吗?
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:signOnResponse xmlns:ns2="http://www.verimatrix.com/omi">
<sessionHandle>
<ns1:handle xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">gVwAFRVlyqRKgqk/u73Ifc3nGig=</ns1:handle>
</sessionHandle>
<result>
<ns1:resultId xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">admin</ns1:resultId>
<ns1:resultCode xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">0</ns1:resultCode>
<ns1:resultText xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">Success</ns1:resultText>
</result>
</ns2:signOnResponse>
</soapenv:Body>
</soapenv:Envelope>
最终,我想解析 object 以从 ns1:handle 获取 session 句柄信息
在加载和遍历文档时,您需要注意名称空间。 阅读simplexml_load_string和SimpleXMLElement class。
<?php
$response = <<<END
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns2:signOnResponse xmlns:ns2="http://www.verimatrix.com/omi">
<sessionHandle>
<ns1:handle xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">gVwAFRVlyqRKgqk/u73Ifc3nGig=</ns1:handle>
</sessionHandle>
<result>
<ns1:resultId xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">admin</ns1:resultId>
<ns1:resultCode xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">0</ns1:resultCode>
<ns1:resultText xmlns:ns1="http://www.verimatrix.com/schemas/OMItypes.xsd">Success</ns1:resultText>
</result>
</ns2:signOnResponse>
</soapenv:Body>
</soapenv:Envelope>
END;
$unmarshalled = simplexml_load_string($response, null, null, 'soapenv', true);
if ($unmarshalled !== false)
{
$handle = $unmarshalled->Body->children('ns2', true)->signOnResponse->children()->sessionHandle->children('ns1', true)->handle;
assert($handle=='gVwAFRVlyqRKgqk/u73Ifc3nGig=', 'Handle should be gVwAFRVlyqRKgqk/u73Ifc3nGig=');
echo 'Handle is: '.$handle.PHP_EOL;
}
else
{
echo 'Invalid XML'.PHP_EOL;
}
这是一个很常见的陷阱:例如simplexml_load_string
返回的 object 在var_dump()
上似乎是空的,而实际上它不是(顺便说一句,它甚至不是 object,而是一个资源)。
让我们尝试通过从文档中获取一些节点来证明这一点。 作为 XPath 的忠实粉丝,我将为此使用 XPath 表达式,但首先我们必须处理命名空间:
$xml = simplexml_load_string($xml);
// Get all namespaces
$namespaces = $xml->getNamespaces(true);
// Register them so XPath knows about them
foreach ($namespaces as $prefix => $url) {
$xml->registerXPathNamespace($prefix, $url);
}
有了这个,我们可以提前 go 并获取一些节点:
// Grab all nodes with a namespace of 'ns1'. To just get the node
// you're after, you could just say $xml->xpath('//ns1:handle').
$query = $xml->xpath('//ns1:*');
// Prepare to collect some infos about our fetched nodes (if any).
$collect = [];
// Loop the result set given by XPath, if any.
foreach ($query as $node) {
$collect[] = [
// Name of the node without namespace prefix, e.g. 'resultId'
$node->getName(),
// Value, e.g. 'admin'
(string) $node,
// Namespace, e.g. 'ns1'
$node->getNamespaces()
];
}
现在,当您的转储collect
时,您正在转储具有实际值的实际数组。 而且它不是空的。
参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.