探讨如何使用SimpleXML函数来加载和解析XML文档

yipeiwu_com5年前PHP代码库
大量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."
    }
  }
}

相关文章

最新用php获取谷歌PR值算法,附上php查询PR值代码示例

复制代码 代码如下: /* *功能:对URL进行编码 *参数说明:$web_url 网站URL,不包含"http://",例如jb51.net */ function HashURL($...

php获取四位字母和数字的随机数的实现方法

那么我们知道在php中简单的四位数的纯数字验证可以用rand(1000,9999)就可以了,但如果我们要得到字母和数字的随机四位数,那我们该如何写函数呢?下面胡鹏博客在php资料栏目下给...

PHP实现的简单网络硬盘

本文实例讲述了PHP实现的简单网络硬盘。分享给大家供大家参考。具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi...

如何用C语言编写PHP扩展的详解

如何用C语言编写PHP扩展的详解

1:预定义在home目录,也可以其他任意目录,写一个文件,例如caleng_module.def内容是你希望定义的函数名以及参数:int a(int x,int y)string b(s...

PHP微信开发用Cache 解决数据缓存

用php进行微信开发时,碰到access_token长久保存的问题,以前都是用框架里的Cache直接set、get一下就完了。现在没框架可用了,只好自己动手写一个cache暂时用。 这个...