转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, $value, array $attrs) //增加本节点的属性
$document->addChild (
string
$name, [
array
$attrs =
array()], [
int
$level =
null])//增加一个子节点及其属性,返回子节点(JSimpleXMLElement类型)
$document->attributes ([
string
$attribute =
null])//获取节点的属性,若无参数,则返回属性/值的关联数组
$document-> children() //获取所有的子元素
$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. & 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节点的常用操作,包括访问子节点,指定属性,指定节点,增加及设置子节点等等