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 DOM
使用 DOM 库、SAX 解析器和正则表达式
文档选项
打印本页
将此页作为电子邮件发送
级别: 中级
Jack Herrington (jack_d_herrington@codegeneration.net), 高级软件工程师, "Code Generation Network"
2006 年 2 月 06 日
有许多技术可用于用 PHP 读取和编写 XML。本文提供了三种 ......
PHP支持8种原始类型,其中包括:
4种标量类型:boolean(布尔型)、integer(整型)、float(浮点型)、string(字符型);
2种复合类型:array(数组)、object(对象);
2种特殊类型:resource(资源)、NULL
您可能还会读到一些关于“双精度(double)”类型的参考。实际上 double 和 float 是相同的,由于一些历史的原 ......
最近在学习php,页面已经做好了,现在就是和虚拟机里C代码的通信,socket编程还可以,就是拿数据的处理,难,。
类的编程我不会,没有学过C++。本来打算用结构体的,可是PHP里面居然没有结构体,怎么办呢?继续努力吧
......
PHP缓存代码
好的页面缓存代码,可以减轻CPU和MYSQL负担。使用前,先在根目录创建“cache”文件夹,然后运行1.php,第一次运行和第二次运行速度差异很大。欢迎熟悉PHP的朋友使用和提意见。
使用方法:(请保存为temp.php)
<?php
include('arrcache.php');
$cache = new ArrCache('cache',5,'txt');
......
<!-- xml格式
<foo xmlns="test">
<bar attr='a'></bar>
<bar attr='b'></bar>
<bar attr='c'></bar>
</foo>
-->
<?php
$dom = new DOMDocument();
if (!$dom->load('attr.xml'))
{
echo "load books.xml failed!<br>";
re ......