我正在嘗試顯示來自 SOAP API 的多條記錄。我的電話工作正常,這是預(yù)期的 XML 響應(yīng):<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetDataResponse xmlns="urn:com:esi911:webeoc7:api:1.0"> <GetDataResult> <data> <record dataid="6" county="Fayette County" title="Title goes here" description="Description goes here." status="Inactive" /> <record dataid="5" county="Caldwell County" title="Title goes here" description="Description goes here." status="Inactive" /> <record dataid="4" county="Burnet County" title="Title goes here" description="Description goes here." status="Active" /> <record dataid="2" county="Blanco County" title="Title goes here" description="Description goes here." status="Active" /> <record dataid="1" county="Bastrop County" title="Title goes here" description="Description goes here." status="Active" /> </data> </GetDataResult> </GetDataResponse> </soap:Body></soap:Envelope>現(xiàn)在,我只想在我的網(wǎng)頁上顯示這些記錄中的第一個及其屬性。我將此響應(yīng)扔到 SimpleXML 中,并嘗試獲取要顯示的多個屬性。這是我迄今為止基于各種其他 StackOverflow 示例所做的嘗試,但沒有一個真正符合我上面的確切 XML 響應(yīng)結(jié)構(gòu):$xml = simplexml_load_string($response);$response = $xml->xpath("//soap:Body/*")[0];$result = $response->children("urn:com:esi911:webeoc7:api:1.0");echo (string) $result->data->record[0]->attributes()->dataid;我沒有收到任何具體的錯誤。它總是空白,什么都沒有顯示。最終,我需要遍歷這些記錄以將它們?nèi)匡@示或?qū)⑺鼈兇鎯Φ阶约旱臄?shù)組中以供以后使用,但我似乎什至無法從上面的代碼中回顯任何內(nèi)容。我確定這可能與多個名稱空間有關(guān)?或者只是某個地方的基本錯字?任何有關(guān)此 XML 響應(yīng)的建議都會很棒。謝謝!
2 回答

慕萊塢森
TA貢獻1810條經(jīng)驗 獲得超4個贊
您可以先注冊命名空間并使用單個 xpath 查詢獲得所需的結(jié)果:
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('urn', 'urn:com:esi911:webeoc7:api:1.0');
$records = $xml->xpath('//urn:record');
echo (string)$records[0]->attributes()->dataid;
演示:https ://3v4l.org/f8faC
注意:您可以更準(zhǔn)確并使用類似//urn:GetDataResult/urn:data/urn:record(而不是更短的//urn:record)作為 XPath 查詢,以防您收到的 XML 中的另一個位置可能有記錄。

慕雪6442864
TA貢獻1812條經(jīng)驗 獲得超5個贊
您缺少<GetDataResult>
元素級別,您獲取<soap:Body>
標(biāo)簽,然后在"urn:com:esi911:webeoc7:api:1.0"
命名空間中提取子項 - 這將為您提供<GetDataResponse>
元素,所以......
echo (string) $result->GetDataResult->data->record[0]->attributes()->dataid;
- 2 回答
- 0 關(guān)注
- 111 瀏覽
添加回答
舉報
0/150
提交
取消