2.2 使用JSimpleXML
例程1. 访问 CDATA 节点
<?php
$xml = new JSimpleXML();
$xml->loadFile($file);
echo $xml->movie[0]->plot->data(); // "So this language. It's like..."
?>
例程2. 访问子节点
<?php
$xml = new JSimpleXML();
$xml->loadFile($file);
/* For each <movie> node, we echo a separate <plot>. */
foreach ($xml->movie->children() as $movie) {
echo $movie->plot->data(), '<br />';
}
?>
例程 3. 访问节点属性
<?php
$xml = new JSimpleXML();
$xml->loadFile($file);
/* Access the <rating> nodes of the first movie.
* Output the rating scale, too. */
foreach ($xml->movie[0]->rating->children() as $rating) {
switch((string) $rating->attributes('type')) { // Get attributes as element indices
case 'thumbs':
echo $rating, ' thumbs up';
break;
case 'stars':
echo $rating, ' stars';
break;
}
}
?>
4. Special Considerations
4.3 字符集
请注意字符集,解析器默认是UTF-8的字符集,目前没有方式来改变字符集,除非你修改源码。
| [翻译] Joomla XML 解析器规范 上 |



