我正在嘗試獲取從對 CEBroker WebServices 的呼叫回傳的 XML 字串,例如:
<licensees>
<licensee valid="true" State="FL" licensee_profession="RN"
licensee_number="2676612" state_license_format="" first_name="HENRY" last_name="GEITER" ErrorCode="" Message="" TimeStamp="2/18/2022 6:43:20 PM" />
</licensees>
并決議字串以獲得屬性的值,因為它們總是相同的,并且每個字串只有一個被許可人。XML 字串在 $xmlresponse 中回傳,當我使用 print_r 列印它時它是正確的,但是當我嘗試決議時,我總是什么也得不到。
我試過了
$xml_string = simplexml_load_string($xmlresponse);
print_r ("this is my xml after going through simplexml_load_string" . $xml_string);
//上面的行沒有為 $xml_string 列印任何內容;
$json = json_encode($xml_string);
print_r($json);
$array = json_decode($json,TRUE);
echo "<p>Array contents are as follows</p>";
print_r($array);
var_dump ($array);
echo "<p>Array contents ended!</p>";
我是 XML 新手,只需要知道如何決議節點,在這種情況下,我需要知道如何決議回傳的 XML 資料或 XML 檔案的屬性。
謝謝
uj5u.com熱心網友回復:
如果我正確理解您的問題,請嘗試以下操作,假設有兩個被許可人的回應:
$xmlresponse = '
<licensees>
<licensee valid="true" State="FL" licensee_profession="RN" licensee_number="2676612" state_license_format="" first_name="HENRY" last_name="GEITER" ErrorCode="" Message="" TimeStamp="2/18/2022 6:43:20 PM" />
<licensee valid="false" State="NY" licensee_profession="DC" licensee_number="1234" state_license_format="" first_name="JOHN" last_name="DOE" ErrorCode="" Message="" TimeStamp="1/1/2023 6:43:20 PM" />
</licensees>
';
$xml_string = simplexml_load_string($xmlresponse);
$licensees = $xml_string->xpath("//licensee");
foreach ($licensees as $licensee) {
foreach ($licensee ->xpath('./@*') as $attribute){
echo $attribute." ";
}
echo "\n";
}
輸出:
true FL RN 2676612 HENRY GEITER 2/18/2022 6:43:20 PM
false NY DC 1234 JOHN DOE 1/1/2023 6:43:20 PM
輸出
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/430489.html
