1
在PHP腳本,我從一個XML文件上的外部網站調試獲取的兩個屬性的一個關鍵功能。這些屬性在名爲Channel的標籤內標記爲'code'和'locationCode'。問題是,有時locationCode過帳爲空字符串(「」)或不是由網站渠道我不能使用定義的好,所以我需要通過渠道循環,直到我找到一個非空locationCode字符串。爲此,我創建了一個while循環,但是我當前的實現沒有成功地通過位置代碼循環。有沒有更好的方法來實現這一點?檢查空屬性在解析XML文件
當前代碼:代碼
public function setChannelAndLocation(){
$channelUrl="http://service.iris.edu/fdsnws/station/1/query?net=".$this->nearestNetworkCode.
"&sta=".$this->nearestStationCode."&starttime=2013-06-07T01:00:00&endtime=".$this->impulseDate.
"&level=channel&format=xml&nodata=404";
$channelXml= file_get_contents($channelUrl);
$channel_table = new SimpleXMLElement($channelXml);
$this->channelUrlTest=$channelUrl;
//FIXME: Check for empty locationCode string
$this->channelCode = $channel_table->Network->Station->Channel[0]['code'];
$this->locationCode = $channel_table->Network->Station->Channel[0]['locationCode'];
$i = 1;
while($this->locationCode=''){
$this->channelCode = $channel_table->Network->Station->Channel[$i]['code'];
$this->locationCode = $channel_table->Network->Station->Channel[$i]['locationCode'];
$i++;
}
}
['$ locationCode = $ channel_table->的XPath( '//通道/ @ locationCode [= 「」。!]');'(http://php.net/manual/en/simplexmlelement.xpath.php) – Wrikken
或實際, namspaces在那裏,所以['$ channel_table-> - > registerXPathNamespace( '默認',的 'http://www.fdsn.org/xml/station/1'); $ locationCode = $ channel_table->的xpath('//默認:Channel/@ locationCode [。!=「」]');'](http://php.net/manual/en/simplexmlelement.xpath.php) – Wrikken