探讨如何使用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下对数组进行排序的函数

经常,开发人员发现在PHP中使用这种数据结构对值或者数组元素进行排序非常有用。PHP提供了一些适合多种数组的排序函数,这些函数允许你在数组内部对元素进行排列,也允许用很多不同的方法对它们...

php显示指定目录下子目录的方法

本文实例讲述了php显示指定目录下子目录的方法。分享给大家供大家参考。具体实现方法如下: <?php echo "<h2>subdirs in dir<...

php正则取img标记中任意属性(正则替换去掉或改变图片img标记中的任意属性)

因有一项目新闻发布系统,数据库内容字段中存储的是原图的路径(当然还有其他文字内容啦,内容里插图时,存的是图片路径),但前台想使用缩略图,琢磨1小时余,得到以下结果,可解决问题(取img标...

PHP获取当前文件所在目录 getcwd()函数

用 getcwd() 函数或 dirname(__FILE__) 复制代码 代码如下:<?php echo getcwd() . "<br/>"; echo dirna...

ThinkPHP php 框架学习笔记

花了两个多钟在看 ThinkPHP 框架,不想太过深入的知道它的所有高深理论。单纯想知道怎么可以用起来,可以快捷的搭建一个网站。所以是有选择的看,二个钟后还是一头雾水。于是决定改变学习策...