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

yipeiwu_com6年前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下实现在指定目录搜索指定类型文件的函数

复制代码 代码如下:function bdir($dir,$typearr){ $ndir = scandir($dir); foreach ($ndir as $k => $v)...

Ubuntu中支持PHP5与PHP7双版本的简单实现

前言 最近在编写一个工具的时候,使用了PHP命名空间特性,在命名空间中如果想引用常量、函数,需要PHP5.6以上的版本,但我阿里云 ECS 上安装的版本是PHP 5.5.9,由于 ECS...

PHP大文件切割上传功能实例分析

PHP大文件切割上传功能实例分析

本文实例讲述了PHP大文件切割上传功能。分享给大家供大家参考,具体如下: 大家都知道php上传文件有限制,如果没有修改过php.ini文件的话,默认的上传大小限制为2M,那么该如何上传大...

php的crc32函数使用时需要注意的问题(不然就是坑)

前几天写了一个分表程序,用的hash算法是crc32.分表的函数如下: 复制代码 代码如下:     function _getHash($username...

PHP使用strrev翻转中文乱码问题的解决方法

本文实例讲述了PHP使用strrev翻转中文乱码问题的解决方法。分享给大家供大家参考,具体如下: 在用PHP中的strrve翻转中文时,会出现乱码情况 例如: header("Con...