今天小编就为大家分享一篇关于PHP simplexml_load_string()函数实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
PHP simplexml_load_string() 函数
实例
转换形式良好的 XML 字符串为 SimpleXMLElement 对象,然后输出对象的键和元素:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
?>
定义和用法
simplexml_load_string()
函数转换形式良好的 XML 字符串为 SimpleXMLElement 对象。
语法
simplexml_load_string( _data,classname,options,ns,is_prefix_ );
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
实例 2
输出 XML 字符串中每个子节点的元素名称和数据:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对编程学习网的支持。如果你想了解更多相关内容请查看下面相关链接
沃梦达教程
本文标题为:PHP simplexml_load_string()函数实例讲解
猜你喜欢
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- 用nohup命令实现PHP的多进程 2023-09-02
- laravel通用化的CURD的实现 2023-03-17
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- php微信公众号开发之秒杀 2022-11-23
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- PHP中PDO事务处理操作示例 2022-10-15
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22