Archive for the ‘PHP函数库系列’ Category

PHP中的SimpleXML函数库(SimpleXML functions)

Wednesday, April 16th, 2008

SimpleXMLElement->addAttribute() — 在节点中添加属性
SimpleXMLElement->addChild() — 在SimpleXMLElement对象中添加一个节点
SimpleXMLElement->asXML() — 一个SimpleXMLElement对象转换成一个XML
SimpleXMLElement->attributes() — 获取指定节点的属性
SimpleXMLElement->children() — 获取SimpleXMLElement的子对象
SimpleXMLElement->__construct() — 建立一个SimpleXMLElement对象
SimpleXMLElement->getDocNamespaces() — 获取XML中定义的命名空间
SimpleXMLElement->getName() — 获取XML对象的名称
SimpleXMLElement->getNamespaces() — 获取XML中使用了的命名空间
SimpleXMLElement->registerXPathNamespace() — 注册一个供xpath()方法使用的命名空间
SimpleXMLElement->xpath() — 使用xpath的语法来解析一个SimpleXMLElement对象
simplexml_import_dom — 将一个dom(php内置的DOM Functions)对象转换为一个SimpleXMLElement
simplexml_load_file — 载入一个xml文件并解析为SimpleXMLElement对象
simplexml_load_string — 载入一个字符串并解析为SimpleXMLElement对象

首先建立一个测试用的XML文件test.xml:

<?xml version='1.0' ?>
<b:bookshop xmlns:b="http://www.csphp.com/">
    
<b:book langusge="english" store="10">
        
<b:name>book name</b:name>
        
<b:author>tom</b:author>
        
<b:content>
            
<b:page>200</b:page>
            
<b:character>105000</b:character>
        
</b:content>
    
</b:book>
    
<b:book>
        
<b:name>another book name</b:name>
        
<b:author>jerry</b:author>
        
<b:content>
            
<b:page>300</b:page>
            
<b:character>165000</b:character>
        
</b:content>
    
</b:book>
</b:bookshop>

__construct()方法的作用是建立一个SimpleXMLElement对象,该方法有5个参数,第一个参数表示字符串形式的XML,第二个参数表示一些设置Libxml的参数,第三个参数设置为true的时候说明第一个参数是URL地址,第四个参数表示命名空间,第五个参数不详。

<?php
$xml = new SimpleXMLElement(file_get_contents('test.xml'), null, null, 'http://www.csphp.com/');
print_r($xml);
?>

simplexml_load_file()函数的作用是载入一个xml文件并解析为SimpleXMLElement对象,simplexml_load_file()函数有五个参数,第一个参数表示载入文件的路径,第二个参数表示将解析的结果扩展到某个指定的SimpleXMLElement对象中,第三个参数表示一些设置Libxml的参数,第四个参数是载入的命名空间,第五个参数没有测试成功。

<?php
$xml = simplexml_load_file('test.xml', null, null, 'http://www.csphp.com/');
echo $xml->book->name;
// 显示 book name
?>

simplexml_load_string()函数的参数和作用和simplexml_load_file()函数类似,区别是simplexml_load_string()函数载入的是一个文件流。

<?php
$file_string = file_get_contents('test.xml');
$xml = simplexml_load_string($file_string, null, null, 'http://www.csphp.com/');
echo $xml->book->name;
// 显示 book name
?>

simplexml_import_dom()函数的作用是将一个dom(php内置的DOM Functions)对象转换为一个SimpleXMLElement,simplexml_import_dom()函数有两个参数,第一个参数表示的是DOM对象,第二个参数表示将解析的结果扩展到某个指定的SimpleXMLElement对象中。

$dom = new DOMDocument;
$dom->loadXML('<book><content><page>150</page></content></book>');
$xml = simplexml_import_dom($dom);
echo $xml->content->page;
// 显示 150

getNamespaces()方法的作用是获取XML中使用了的命名空间,getDocNamespaces()方法的作用是获取XML中定义的命名空间。两个函数都有一个参数,设置为true的时候,会递归的获取整个文档中出现的namespace,否则只获取根节点的namespace。

<?php
$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person t:id="1">John Doe</p:person>
    <p:person t:id="2" xmlns:a="http://example.org/addr">
        Susie Q. Public
    </p:person>
</people>
XML;
 
$sxe = new SimpleXMLElement($xml);
print_r($sxe->getNamespaces(true));
print_r($sxe->getDocNamespaces(true));
/**
 * Array
 * (
 *     [p] =>
http://example.org/ns
 *     [t] =>
http://example.org/test
 * )
 * Array
 * (
 *     [p] =>
http://example.org/ns
 *     [t] =>
http://example.org/test
 *     [a] =>
http://example.org/addr
 * )
 */

?>

getName()方法的作用是获取XML对象的名称。

<?php
$xml = simplexml_load_file('test.xml', null, null, 'http://www.csphp.com/');
print_r($xml->getName());
// 显示 bookshop
?>

children()方法的作用是获取SimpleXMLElement的子对象。

<?php
$ns = 'http://www.csphp.com/';
$xml = simplexml_load_file('test.xml', null, null, $ns);
echo $xml->children($ns)->children($ns)->name;
// 显示 book name
?>

attributes()方法的作用是获取指定节点的属性。

<?php
$xml = simplexml_load_file('test.xml', null, null, 'http://www.csphp.com/');
foreach($xml->book->attributes() as $key=>$value)
{
    
echo $key . '=>' . $value."\n";
}
// 显示
// langusge=>english
// store=>10
?>

asXML()方法的作用是将一个SimpleXMLElement对象转换成一个XML,asXML()方法有一个表示文件地址的可选参数,会将XML直接写入这个文件,而不是返回。

addChild()方法的作用是在SimpleXMLElement对象中添加一个节点。addChild()方法有三个参数,第一个参数表示节点的名称,第二个参数表示节点的值,第三个参数表示命名空间。addAttribute()方法的作用是在节点中添加属性,第一个参数表示属性的名称,第二个参数表示属性的值,第三个参数表示命名空间。

<?php
$xml = simplexml_load_file('test.xml', null, null, 'http://www.csphp.com/');
$xml->book->addChild('time', '20080503');
$xml->book->addAttribute('amount', '$63');
print_r($xml->asXML());
/**
 * <?xml version="1.0"?>
 * <b:bookshop xmlns:b="
http://www.csphp.com/">
 *     <b:book langusge="english" store="10" amount="$63">
 *         <b:name>book name</b:name>
 *         <b:author>tom</b:author>
 *         <b:content>
 *             <b:page>200</b:page>
 *             <b:character>105000</b:character>
 *
 *         </b:content>
 *     <b:time>20080503</b:time></b:book>
 *     <b:book>
 *         <b:name>another book name</b:name>
 *         <b:author>jerry</b:author>
 *         <b:content>
 *             <b:page>300</b:page>
 *
 *             <b:character>165000</b:character>
 *         </b:content>
 *     </b:book>
 * </b:bookshop>
 *
 */

?>

xpath()方法的使用是使用xpath的语法来解析一个SimpleXMLElement对象,xpath()有一个参数,表示一个xpath语法的字符串,registerXPathNamespace()方法的作用是注册一个供xpath()方法使用的命名空间,registerXPathNamespace()方法有两个参数,第一个表示名称,第二个表示的是值。

<?php
$xml = simplexml_load_file('test.xml', null, null, 'http://www.csphp.com/');
$xml->registerXPathNamespace('anotherb', 'http://www.csphp.com/');
$result = $xml->xpath('//anotherb:page');
print_r($result);
/**
 * Array
 * (
 *     [0] => SimpleXMLElement Object
 *         (
 *             [0] => 200
 *         )
 *
 *     [1] => SimpleXMLElement Object
 *         (
 *             [0] => 300
 *         )
 *
 * )
 */

?>

PHP中的Memcache函数库(Memcache Functions)

Wednesday, March 19th, 2008

PHP中的错误处理和日志函数(Error Handling and Logging Functions)

Monday, March 17th, 2008

PHP中的数学函数(Mathematical Functions)

Saturday, December 22nd, 2007

PHP中处理Session的函数(Session Handling Functions)

Friday, May 25th, 2007