实例介绍joomla! xml解析器的使用 【转】

http://www.drupaling.cn/cms/joomla/165-joomla-xml.html
众所周知的是Joomla是采用domit来解析XML的,并且做了一些封装。在工厂兼管家JFactory类中提供了获取解析器的get方法。自joomla!1.5 后,提供了从domit封装来的三种解析器,包括dom,rss/atom,simple。要使用xml解析器,首先得得到一个xmlparser类,方法很简单:

$xmlparser = &JFactory::getXMLParser('Simple');

其中JFactory::getXmlParser()如果不传参数则将默认返回dom型的xml解析器
$xmlparser 为JSimpleXML类型,可以直接解析xml字符串或者一个正确的xml文件,方法分别是loadString()和loadFile(),本文实例为字符串。

$xml = '<?xml version="1.0" ?>
<catalogue name="Some Music Collection">
<album>
<title>Moving Pictures</title>
<artist>Rush</artist>
<year>1981</year>
<tracks>
<track length="4:33">Tom Sawyer</track>
<track length="6:06">Red Barchetta</track>
<track length="4:24">YYZ</track>
<track length="4:19">Limelight</track>
<track length="10:56">The Camera Eye</track>
<track length="4:43">Witch Hunt</track>
<track length="4:43">Vital Signs</track>
</tracks>
</album>
</catalogue>';
$parser = & JFactory::getXMLParser ( 'Simple' );
$parser->loadString ( $xml );
$document = & $parser->document;

执行完loadString()后,$parser的document属性即为整个xml根结点(JSimpleXMLElement类型),得到了根结点,那么接下来的操作就变得非常简单了,Joomla的node操作接口主要包括以下:
$document->
addAttribute ( $name$valuearray  $attrs) //增加本节点的属性
$document->addChild ( string  $name, [ array  $attrs =  array()], [ int  $level =  null])//增加一个子节点及其属性,返回子节点(JSimpleXMLElement类型)
$document->attributes ([ string  $attribute =  null])//获取节点的属性,若无参数,则返回属性/值的关联数组
$document-> children() //获取所有的子元素
$document-> data ()//返回节点的值
$document-> getElementByPath ( string  $path)//根据指定的path得到指定节点
$document-> name ()//获取节点的标签名
$document-> removeAttribute ( string  $name)//删除指定属性
$document-> removeChild (  &$child)//删除指定子节点
$document-> setData ( string  $data)//设置节点值
$document-> toString ([  $whitespace =  true])//返回本节点及子节点的字符串表示

简单应用如下:

$newAlbum = $document->addChild ('album');
$title = $newAlbum->addChild ( 'title' );
$artist = $newAlbum->addChild ( 'artist' );
$year = $newAlbum->addChild ( 'year' );
$tracks = $newAlbum->addChild ( 'tracks' );
$title->setData ( 'Green Onions' );
$artist->setData ( 'Booker T. &amp; The MG/'s' );
$year->setData ( '1962' );
$track = & $tracks->addChild ( 'track', array ('length' => '1.45' ) );
$track->setData ( 'Green Onions' );

$albums = & $document->album;

for($i = 0, $c = count ( $albums ); $i < $c; $i ++) {
    // get the album
    $album = & $albums [$i];
    echo '<hr/><div>';
    if ($name = & $album->getElementByPath ( 'title' )) {
        // display title
        echo '<strong>' . $name->data () . '</strong><br/>';
    }
    if ($artist = & $album->getElementByPath ( 'artist' )) {
        // display the artist
        echo '<em>' . $artist->data () . '</em>';
    }
    if ($year = & $album->getElementByPath ( 'year' )) {
        // display the year of release
        echo ' (' . $year->data () . ')';
    }
    
    if ($tracks = & $album->getElementByPath ( 'tracks' )) {
        // get the track listing
        $listing = & $tracks->track;
        // output listing table
        echo '<table><tr><th>Track</th><th>Length</th></tr>';
        for($ti = 0, $tc = count ( $listing ); $ti < $tc; $ti ++) {
            // output an individual track
            $track = & $listing [$ti];
            echo '<tr>';
            echo '<td>' . $track->data () . '</td>';
            echo '<td>' . $track->attributes ( 'length' ) . '</td>';
            echo '</tr>';
        }
        echo '</table>';
    }
    echo '</div>';
}
实例中应用了xml节点的常用操作,包括访问子节点,指定属性,指定节点,增加及设置子节点等等

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值