Joomla!-开源天空

2008-09-08
首页 专栏热点 Joomla! 源代码分析 [翻译] Joomla XML 解析器规范 上


[翻译] Joomla XML 解析器规范 上

E-mail

概述

Joomla! 1.5 新增 JSimpleXML 类,可以更为简单的读取xml文件,但是现在还不能修改CDATA或或者属性,也不支持XPath.

JSimpleXML需要PHP 4.2以上版本,以及 Expat扩展。


2. 实现

JSimpleXML 类代码:


class JSimpleXML extends JObject
{
    /**
    * Document element
    *
    * @var object
    */
    var $document = null;
   
     /**
     * Interprets a string of XML into an object
     *
     * This function will take the well-formed xml string data and return an object of class
     * JSimpleXMLElement with properties containing the data held within the xml document.
     * If any errors occur, it returns FALSE.
     *
     * @param string  Well-formed xml string data
     * @param string  currently ignored
     * @return object JSimpleXMLElement
     */
    function loadString($string, $classname = null)
    {
  $this->_parse($string);
  return true;
    }

     /**
     * Interprets an XML file into an object
     *
     * This function will convert the well-formed XML document in the file specified by
     * filename to an object  of class JSimpleXMLElement. If any errors occur during file
     * access or interpretation, the function returns FALSE.
     *
     * @param string  Path to xml file containing a well-formed XML document
     * @param string  currently ignored
     * @return object JSimpleXMLElement
     */
    function loadFile($path, $classname = null)
    {
  //Get the XML document loaded into a variable
  $xml = file_get_contents($path);
  $this->_parse($xml);
  return true;
    }

    /**
     * Get the parser
     *
     * @access public
     * @return resource XML parser resource handle
     */
    function getParser() {
  return $this->_parser;
    }

    /**
     * Set the parser
     *
     * @access public
     * @param resource XML parser resource handle
     */
    function setParser($parser) {
  $this->_parser = $parser;
    }
}

3. 使用

正如其名,JSimpleXML使用起来非常简单。你可以参考php5的SimpleXML文档,机关本质上两者有些不同,但是使用上是基本兼容的。

来看一个xml文档的例子(example.xml)

<?xml version='1.0' standalone='yes' ?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
    <character>
     <name>Ms. Coder</name>
     <actor>Onlivia Actora</actor>
    </character>
    <character>
     <name>Mr. Coder</name>
     <actor>El ActÓr</actor>
    </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a scripting language? All  
   is revealed in this thrilling horror spoof of a documentary.
  </plot>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>

2.1 开始

首先要载入xml文档,在本例中我们要载入example.xml,JSimpleXML loadFile 方法载入相应文件的xml格式数据。loadFile其实使用 file_get_contents()方法读取文件,并把被荣传递给解析器。

<?php
//Create a JSimpleXML object
$xml = new JSimpleXML();

//Load the xml file
$xml->loadFile($file);
?>

XML解析器在解析文档时候发生错误会触发JError,如果没有任何错误,就会继续进行。

JSimpleXML的对象结构是直接了当的,文档的根包含在内。也就是说上例中$xml->document是根。$xml->document->movie[0]是第一个movie标签$xml->document->movie是一个数组,而不是JSimpleXMLElement对象。

相关文章:
[翻译] Joomla XML 解析器规范 下

发表您的文章评论

您的姓名 (昵称)
标题:
评分: 很差一般较好很好
评论:
验证码:
请输入验证码