探讨如何使用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实现将字符串按照指定距离进行分割的方法。分享给大家供大家参考。具体如下: 将一个字符串每隔三个字符添加一个逗号,例如把字符串1234567890转换为1,234,56...

php中json_decode()和json_encode()的使用方法

1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 J...

php中10个不同等级压缩优化图片操作示例

本文实例分析了php中10个不同等级压缩优化图片操作。分享给大家供大家参考,具体如下: 今天找到一个php写的压缩图片程序,可以分10个等级(0-9)来压缩,0等级时压缩比率不是很大,图...

php引用返回与取消引用的详解

一、引用返回引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。不要用返回引用来增加性能,引擎足够聪明来自己进行优化。仅在有合理的技术原因时才返回引用!要返回引用,使用此语法:复...

php中ob(Output Buffer 输出缓冲)函数使用方法

来自:http://bbs.phome.net/ShowThread/?threadid=9247&forumid=2  在PHP编程中,  我们经...