Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析XML文件时检查空属性_Php_Xml_Simplexml - Fatal编程技术网

Php 解析XML文件时检查空属性

Php 解析XML文件时检查空属性,php,xml,simplexml,Php,Xml,Simplexml,我正在调试的PHP脚本中的一个关键函数是从外部站点上的XML文件中获取get的两个属性。这些属性在名为Channel的标记中标记为“code”和“locationCode”。问题是,有时locationCode被发布为空字符串(“”),或者站点根本没有为我无法使用的频道定义,因此我需要在这些频道中循环,直到找到一个非空locationCode字符串。为此,我创建了一个while循环,但我当前的实现没有成功地循环位置代码。有没有更好的方法来实现这一点 当前代码: public function s

我正在调试的PHP脚本中的一个关键函数是从外部站点上的XML文件中获取get的两个属性。这些属性在名为Channel的标记中标记为“code”和“locationCode”。问题是,有时locationCode被发布为空字符串(“”),或者站点根本没有为我无法使用的频道定义,因此我需要在这些频道中循环,直到找到一个非空locationCode字符串。为此,我创建了一个while循环,但我当前的实现没有成功地循环位置代码。有没有更好的方法来实现这一点

当前代码:

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++;
    }
}

代码的示例XML文件:

这行代码中有两个问题:

while($this->locationCode=''){
首先,您输入了一个作业(
=
),而您需要的是一个比较(
=
)。因此,该行不是测试条件,而是过度写入
$this->locationCode
的当前值,然后测试
'
的“真实性”,其计算结果为
false
,因此
while
循环永远不会运行

其次,示例XML文件显示该属性实际上不是空的,而是包含一些空格。假设这些是您想要忽略的值(目前样本中没有任何其他值),您可以使用来消除比较中的空白,这样做:

while( trim($this->locationCode) == '' ) {

这一行有两个问题:

while($this->locationCode=''){
首先,您输入了一个作业(
=
),而您需要的是一个比较(
=
)。因此,该行不是测试条件,而是过度写入
$this->locationCode
的当前值,然后测试
'
的“真实性”,其计算结果为
false
,因此
while
循环永远不会运行

其次,示例XML文件显示该属性实际上不是空的,而是包含一些空格。假设这些是您想要忽略的值(目前样本中没有任何其他值),您可以使用来消除比较中的空白,这样做:

while( trim($this->locationCode) == '' ) {

或者事实上,那里的空间,所以或者实际上,那里的空间,所以