探讨如何使用SimpleXML函数来加载和解析XML文档
程序员文章站
2023-04-07 16:18:49
大量smiplexml函数可用来加载和解析大量xml文档。---------------------------------------------------------...
大量smiplexml函数可用来加载和解析大量xml文档。
--------------------------------------------------------------------------------
1.simplexml_load_file()函数来加载指定的xml文件到对象。如果加载文件时遇到问题,则返回flase。例:
book.xml文件:
<?xml version="1.0" standalone="yes"?>
<library>
<book>
<title>pride and prejudice</title>
<author gender="female">jane austen</author>
<description>jane austen's most popular work.</description>
</book>
<book>
<title>the conformist</title>
<author gender="male">alberto moravia</author>
<description>alberto moravia's classic psyhcological novel.</description>
</book>
<book>
<title>the sun also rises</title>
<author gender="male">ernest hemingway</author>
<description>the masterpiece that launched hemingway's career.</description>
</book>
</library>
php文件:
<?php
$xml=simplexml_load_file("book.xml");echo "<pre>";
var_dump($xml);
?>
输出结果:
object(simplexmlelement)#1 (1) {
["book"]=>
array(3) {
[0]=>
object(simplexmlelement)#2 (3) {
["title"]=>
string(19) "pride and prejudice"
["author"]=>
string(11) "jane austen"
["description"]=>
string(32) "jane austen's most popular work."
}
[1]=>
object(simplexmlelement)#3 (3) {
["title"]=>
string(14) "the conformist"
["author"]=>
string(15) "alberto moravia"
["description"]=>
string(46) "alberto moravia's classic psyhcological novel."
}
[2]=>
object(simplexmlelement)#4 (3) {
["title"]=>
string(18) "the sun also rises"
["author"]=>
string(16) "ernest hemingway"
["description"]=>
string(49) "the masterpiece that launched hemingway's career."
}
}
}
--------------------------------------------------------------------------------
1.simplexml_load_file()函数来加载指定的xml文件到对象。如果加载文件时遇到问题,则返回flase。例:
book.xml文件:
复制代码 代码如下:
<?xml version="1.0" standalone="yes"?>
<library>
<book>
<title>pride and prejudice</title>
<author gender="female">jane austen</author>
<description>jane austen's most popular work.</description>
</book>
<book>
<title>the conformist</title>
<author gender="male">alberto moravia</author>
<description>alberto moravia's classic psyhcological novel.</description>
</book>
<book>
<title>the sun also rises</title>
<author gender="male">ernest hemingway</author>
<description>the masterpiece that launched hemingway's career.</description>
</book>
</library>
php文件:
复制代码 代码如下:
<?php
$xml=simplexml_load_file("book.xml");echo "<pre>";
var_dump($xml);
?>
输出结果:
复制代码 代码如下:
object(simplexmlelement)#1 (1) {
["book"]=>
array(3) {
[0]=>
object(simplexmlelement)#2 (3) {
["title"]=>
string(19) "pride and prejudice"
["author"]=>
string(11) "jane austen"
["description"]=>
string(32) "jane austen's most popular work."
}
[1]=>
object(simplexmlelement)#3 (3) {
["title"]=>
string(14) "the conformist"
["author"]=>
string(15) "alberto moravia"
["description"]=>
string(46) "alberto moravia's classic psyhcological novel."
}
[2]=>
object(simplexmlelement)#4 (3) {
["title"]=>
string(18) "the sun also rises"
["author"]=>
string(16) "ernest hemingway"
["description"]=>
string(49) "the masterpiece that launched hemingway's career."
}
}
}
上一篇: Linux系统的启动过程
下一篇: 简单的过滤字符串中的HTML标记