到目前为止,我所拥有的是下面的代码。我认为问题在于$bodies = $xml->xpath('domain:cd');
,我不知道如何定义路径。
尝试查看一些示例,但无法弄清楚。
XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0
epp-1.0.xsd">
<response>
<result code="1000">
<msg>Command completed successfully</msg>
</result>
<resData>
<domain:chkData xmlns:domain="urn:ietf:params:xml:ns:domain-1.0"
xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0
domain-1.0.xsd">
<domain:cd>
<domain:name avail="0">domain001.gr</domain:name>
<domain:reason>In Use.</domain:reason></domain:cd>
</domain:chkData>
</resData>
PHP 代码
$xml = simplexml_load_string($result, NULL, NULL, 'urn:ietf:params:xml:ns:epp-1.0');
$xml->registerXPathNamespace('domain', 'urn:ietf:params:xml:ns:domain-1.0');
$bodies = $xml->xpath('/resData/domain:chkData');
echo "alitheia";
foreach($bodies as $body){
$reply = $body->children('domain', TRUE)->cd;
$nameout =(string)$reply->name;
echo $nameout;
echo "alitheia2";
}
“alitheia”回声用于调试以查看我的代码到达的位置。“Altheia2”从未出现。
解决此问题的代码以防其他人提出此问题
//i loaded the xml in the p2xml variable using file_get_contents
$p2xml = new SimpleXmlElement($p2xmlf);
foreach ($p2xml->response->resData $entry2)
{
$namespaces = $entry2->getNameSpaces(true);
$dc = $entry2->children($namespaces['domain']);
$nameout = $dc->chkData->name;
//below is what i used to get the attribute
$attrout = $dc->chkData->name->attributes();
$oxml = $p2xml->asXML();
}