探讨如何使用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."
    }
  }
}

相关文章

Yii2设置默认控制器的两种方法

Yii2设置默认控制器的两种方法

本文主要给大家介绍了关于Yii2默认控制器设置的内容,分享了两种方法供大家参考学习,下面来一起看看详细的介绍: 方法1: 首先Yii2中在/vendor/yiisoft/yii2/web...

PHP自动识别字符集并完成转码详解

因为自己使用字符编码一般的是utf-8编码,但如果对方的博客使用gb2312编码的话,POST过来就会出现乱码(除非对方POST前先转换编码)。在不能保证对方是否一定使用utf-8编码的...

PHP排序算法之简单选择排序(Simple Selection Sort)实例分析

本文实例讲述了PHP排序算法之简单选择排序(Simple Selection Sort)。分享给大家供大家参考,具体如下: 基本思想: 通过 n - i 次关键字间的比较,从 n - i...

php利用iframe实现无刷新文件上传功能的代码

复制代码 代码如下: <html>   <head>   <title>无刷新上传文件</title>   <meta Conten...

PHP中SESSION使用中的一点经验总结

PHP中SESSION使用中的一点经验总结

SESSION会话开启时,会首先发送一个对浏览器的唯一标识session_id的cookie(名字为PHPSESSID可以通过session_name()获取),同session.sav...