当前位置: 移动技术网 > IT编程>开发语言>PHP > 探讨如何使用SimpleXML函数来加载和解析XML文档

探讨如何使用SimpleXML函数来加载和解析XML文档

2019年04月10日  | 移动技术网IT编程  | 我要评论
大量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."
    }
  }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网