PHP:需要有关简单 XML 的帮助!
我是 PHP 初学者。我正在尝试解析这个 xml 文件。
<relationship>
<target>
<following type="boolean">true</following>
<followed_by type="boolean">true</followed_by>
<screen_name>xxxx</screen_name>
<id type="integer">xxxx</id>
</target>
<source>
<notifications_enabled nil="true"/>
<following type="boolean">true</following>
<blocking nil="true"/>
<followed_by type="boolean">true</followed_by>
<screen_name>xxxx</screen_name>
<id type="integer">xxxxx</id>
</source>
</relationship>
我需要获取目标的“following type="boolean””字段的值,这是我的代码 -
$xml = simplexml_load_string($response);
foreach($xml->children() as $child)
{
if ($child->getName() == 'target')
{
foreach($child->children() as $child_1)
if ( $child_1->getName() == 'following')
{
$is_my_friend = (bool)$child_1;
break;
}
break;
}
}
但我没有得到正确的输出。我认为该字段的“ type =“boolean””部分正在产生问题。请帮忙。
I am beginner in PHP. I am trying to parse this xml file.
<relationship>
<target>
<following type="boolean">true</following>
<followed_by type="boolean">true</followed_by>
<screen_name>xxxx</screen_name>
<id type="integer">xxxx</id>
</target>
<source>
<notifications_enabled nil="true"/>
<following type="boolean">true</following>
<blocking nil="true"/>
<followed_by type="boolean">true</followed_by>
<screen_name>xxxx</screen_name>
<id type="integer">xxxxx</id>
</source>
</relationship>
I need to get the value of the field 'following type="boolean" ' for the target and here's my code -
$xml = simplexml_load_string($response);
foreach($xml->children() as $child)
{
if ($child->getName() == 'target')
{
foreach($child->children() as $child_1)
if ( $child_1->getName() == 'following')
{
$is_my_friend = (bool)$child_1;
break;
}
break;
}
}
but I am not getting the correct output. I think the ' type="boolean" ' part of the field is creating problems. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 xpath 来实现此目的。
You could also use xpath for this.
$xml = simplexml_load_string($response);
foreach($xml->目标->以下为$child)
{
$is_my_friend = $child;
}
$xml = simplexml_load_string($response);
foreach($xml->target->following as $child)
{
$is_my_friend = $child;
}
在 PHP 中将字符串转换为布尔值时,除了空字符串和“0”之外的所有值都被视为 TRUE。
http://www.php。 net/manual/en/language.types.boolean.php#language.types.boolean.casting
When casting a string to boolean in PHP, all values except the empty string and "0" are considered TRUE.
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting