PHP simplexml_load_string()函数实例讲解

作者:php参考手册 时间:2023-07-09 07:19:28 

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 simplexml_load_string()函数实例讲解

PHP simplexml_load_string()函数实例讲解

实例 1

输出 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->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(),函数
0
投稿

猜你喜欢

  • 避免使用滤镜

    2009-10-13 20:30:00
  • 几个比较重要的MySQL变量

    2024-01-23 20:22:36
  • python 实现list或string按指定分段

    2023-10-30 02:04:20
  • Golang 操作TSV文件的实战示例

    2023-07-18 06:46:01
  • js创建一个input数组并绑定click事件的方法

    2023-08-15 02:35:09
  • Python pip 常用命令汇总

    2023-06-10 06:11:38
  • django中的图片验证码功能

    2022-06-10 00:07:54
  • 针对SQL Server中业务规则链接的分析

    2009-01-20 11:43:00
  • element-ui table span-method(行合并)的实现代码

    2024-05-10 14:17:35
  • sqlserver 数据库学习笔记

    2024-01-14 21:54:33
  • 一起来了解python的运算符

    2022-08-29 03:01:17
  • Python光学仿真理解Jones矩阵学习

    2022-03-10 21:11:39
  • JavaScript对象学习经验整理

    2024-04-17 10:09:50
  • Python爬取腾讯视频评论的思路详解

    2021-05-30 23:04:43
  • 基于python实现聊天室程序

    2022-09-26 07:50:33
  • 微信小程序 支付功能实现PHP实例详解

    2023-11-19 08:53:36
  • Python命令行运行文件的实例方法

    2023-05-10 13:57:56
  • Python演化计算基准函数详解

    2021-02-13 19:55:32
  • python时间序列按频率生成日期的方法

    2021-10-04 22:38:40
  • 简单的Apache+FastCGI+Django配置指南

    2021-01-27 10:09:16
  • asp之家 网络编程 m.aspxhome.com