php解析xml示例
<!-- xml 格式
<books>
<book id='1001'>
<author>andylin</author>
<title>c language</title>
<publisher id="aaa">O'Reilly</publisher>
</book>
<book id='1002'>
<author>congfeng</author>
<title>C++ Designer</title>
<publisher id='bbb'>New Publish</publisher>
</book>
</books>
-->
<?php
$dom = new DOMDocument();
if (!$dom->load('books.xml'))
{
echo "load books.xml failed!<br>";
return;
}
$books = $dom->getElementsByTagName('book');
foreach ($books as $book)
{
//get book id
$book_id = $book->getAttribute('id');
//get author
$nodeAuth = $book->getElementsByTagName('author');
$strAuth = $nodeAuth->item(0)->nodeValue;
//get publisher
$nodePub = $book->getElementsByTagName('publisher');
$strPub = $nodePub->item(0)->nodeValue;
$pub_id = $nodePub->item(0)->getAttribute('id');
//get title
$nodeTitle = $book->getElementsByTagName('title');
$strTitle = $nodeTitle->item(0)->nodeValue;
//save data
$arrInfo['book_id'] = $book_id;
$arrInfo['author'] = $strAuth;
$arrInfo['publiser'] = $strPub;
$arrInfo['title'] = $strTitle;
$arrInfo['pub_id'] = $pub_id;
//save info
$arrInfos[] = $arrInfo;
}
var_dump($arrInfos);
?>
相关文档:
使用 PHP 处理 XML 配置文件
使用 XML 配置文件轻易地配置 PHP 应用程序和对象
级别: 中级
Vikram Vaswani, 创始人, Melonfire
2007 年 11 月 29 日
XML 为应用程序配置文件提供了一种便捷、易用的表达语言。但有时候将这些信息提取到 PHP 脚本中将会面对一个不小的挑战。这正是 XJConf for PHP 包出现的原因:它提 ......
PHP支持8种原始类型,其中包括:
4种标量类型:boolean(布尔型)、integer(整型)、float(浮点型)、string(字符型);
2种复合类型:array(数组)、object(对象);
2种特殊类型:resource(资源)、NULL
您可能还会读到一些关于“双精度(double)”类型的参考。实际上 double 和 float 是相同的,由于一些历史的原 ......
PHP缓存代码
好的页面缓存代码,可以减轻CPU和MYSQL负担。使用前,先在根目录创建“cache”文件夹,然后运行1.php,第一次运行和第二次运行速度差异很大。欢迎熟悉PHP的朋友使用和提意见。
使用方法:(请保存为temp.php)
<?php
include('arrcache.php');
$cache = new ArrCache('cache',5,'txt');
......