探讨如何使用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数组函数序列之array_key_exists() - 查找数组键名是否存在

array_key_exists() 定义和用法 array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 fal...

php中OR与|| AND与&amp;&amp;的区别总结

php中OR与|| AND与&amp;&amp;的区别总结

本身没有区别,习惯问题 ,但是有时候牵涉到运算符优先级的问题,结果会不同,记录下。 例如: 复制代码 代码如下:$p = 6 or 0; var_dump($p);//int(6) $p...

腾讯微博提示missing parameter errorcode 102 错误的解决方法

本文实例讲述了腾讯微博提示missing parameter errorcode 102 错误的解决方法。分享给大家供大家参考。具体分析如下: 今天在调试腾讯微博接口时,出现一个错误,找...

php中isset与empty函数的困惑与用法分析

本文实例讲述了php中isset与empty函数的困惑与用法。分享给大家供大家参考,具体如下: 在学习php有一段时间之后,感觉自己的基础知识还是有点不牢固,有的问题就不怎么知道,比如就...

php数组函数序列之next() - 移动数组内部指针到下一个元素的位置,并返回该元素值

next() 定义和用法 next() 函数把指向当前元素的指针移动到下一个元素的位置,并返回该元素的值。 如果内部指针已经超过数组的最后一个元素,函数返回 false。 语法 next...