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

概述

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)





Ms. Coder
Onlivia Actora


Mr. Coder
El ActÓr



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.

7
5


2.1 开始

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

//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对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值