探讨如何使用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异步进程助手async-helper

简介 PHP 的异步进程助手,借助于 AMQP 实现异步执行 PHP 的方法,将一些很耗时、追求高可用、需要重试机制的操作放到异步进程中去执行,将你的 HTTP 服务从繁重的业务逻辑中...

PHP JSON格式的中文显示问题解决方法

返回json数据中文显示的问题 上一篇文章中,返回json格式的中文显示成\u5723\u8bde\u8282\u5343\u4e07\u597d\u793c\u5927\u5949\u...

PHP分页显示制作详细讲解

每页多少条记录($PageSize)?   当前是第几页($CurrentPageID)?   现在只要再给我一个结果集,我就可以显示某段特定的结果出来。   至于其他的参数,比如:上一...

PHP获取昨天、今天及明天日期的方法

本文实例讲述了PHP获取昨天、今天及明天日期的方法。分享给大家供大家参考,具体如下: //PHP返回昨天的日期 function get_last_date() { $tomorr...

php通过数组实现多条件查询实现方法(字符串分割)

复制代码 代码如下:<?php $keyword="asp php,jsp"; $keyword=str_replace(" "," ",$keyword); $keyword=s...