Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1278828
  • 博文数量: 315
  • 博客积分: 10397
  • 博客等级: 上将
  • 技术积分: 3731
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 21:21
文章分类

全部博文(315)

文章存档

2015年(10)

2014年(3)

2013年(2)

2012年(8)

2011年(8)

2010年(29)

2009年(59)

2008年(77)

2007年(119)

分类:

2007-11-09 13:19:37

XML目前已经形成事实上的编程接口的标准,一个程序是否具有扩展能力,看其对xml的支持能力就基本一目了然,在IBM Developerworks 看到一篇很有帮助的教程文档,原文地址如下:
http://www.ibm.com/developerworks/cn/xml/x-query2xml/,是PEAR类库的一个xml的组件,功能强大,只要你的数据库设计的OK,就可以依据这个组件从XML中挖掘想要的数据。
OK,闲话少说:
1、安装PEAR和XML_Query2XML以及MDB2和其mysql驱动
 pear install --alldeps XML_Query2XML
 pear install MDB2_Driver_mysql
 
2、先睹为快:

// include required files
include 'XML/Query2XML.php';
include 'MDB2.php';

try {
    // initalize Query2XML object
    $q2x = XML_Query2XML::factory(MDB2::factory('mysql://root:pass@localhost/world'));
   
    // generate SQL query
    // get results as XML
    $sql = "SELECT * FROM Country";
    $xml = $q2x->getFlatXML($sql);
   
    // read XSL stylesheet data
    $xsl = new DOMDocument;
    $xsl->load('country.xsl');
   
    // initialize XSLT engine
    $xslp = new XSLTProcessor;
   
    // attach XSL stylesheet object
    $xslp->importStyleSheet($xsl);
   
    // perform transformation
    header('Content-Type: text/html');
    echo $xslp->transformToXML($xml);
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

对了示例数据库下面提供下载:

文件: world.sql.zip
大小: 89KB
下载: 下载

以及xsl文件:



   
       
           
               
           
           
               


                   
                       
                           
                               

                           
                       
                   
                   
                       
                   

               

                                   
                               

           
       
   

   
       
           
       
   

   
       
           
       
   


3、上面只是简单的提供数据库查询结果转xml并有xsl格式化输出,下面来点特别的:

// include required files
include 'XML/Query2XML.php';
include 'MDB2.php';

try {
    // initalize Query2XML object
    $q2x = XML_Query2XML::factory(MDB2::factory('mysql://root:pass@localhost/world'));
   
    // generate SQL query
    // get results as XML
    $sql_1 = "SELECT * FROM Country";
    $sql_2 = "SELECT * FROM City WHERE CountryCode = ? ORDER BY Population DESC LIMIT 5";
    $xml = $q2x->getXML($sql_1, array(
            'idColumn' => 'code',
            'rootTag' => 'countries',
            'rowTag' => 'country',
            'attributes' => array('code', 'name', 'continent'),
            'elements' => array('cities' => array(
                'sql' => array('data' => array('code'), 'query' => $sql_2),
                'idColumn' => 'id',
                'rootTag' => 'cities',
                'rowTag' => 'city',
                'attributes' => array('name','district','population'))
            )
        )   
    );
   
    // read XSL stylesheet data
    $xsl = new DOMDocument;
    $xsl->load('countries.xsl');
   
    // initialize XSLT engine
    $xslp = new XSLTProcessor;
   
    // attach XSL stylesheet object
    $xslp->importStyleSheet($xsl);
   
    // perform transformation
    header('Content-Type: text/html');
    echo $xslp->transformToXML($xml);
} catch (Exception $e) {
    echo $e->getMessage();
}
?>
注意粗红字体部分,一句话概括的说就是在序列化xml的时候,不只可以用一个sql,还可以把一个sql的查询结果作为第二个sql查询的依据从而生成xml

counties.xsl的代码如下



   
       
           
               
           
           
               
                   

-                     select="@continent"/>


                   
                       
                           
                               
                               

                               
                           
                       
                       
                           
                       

                   

                                   
                               

               
           
       
   

   
     
       
       
           
       
       

   
   


       

阅读(817) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~