易截截图软件、单文件、免安装、纯绿色、仅160KB

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 printf函数简介及格式化规定符介绍

printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息。在编写程序时经常会用到此函数。函数的原型为:
int printf(string $fromat [,mixed $args [,mixed ...]])
函数返回值为整型。若成功则返回输出的字符数,输出出错则返回负值。
printf()函数的调用格式为:
printf("<格式化字符串>", & ......

PHP语言的一些缺陷

1. 对递归的不良支持
递归是一种函数调用自身的机制。这是一种强大的特性可以把某些复杂的东西变得很简单。有一个使用递归的例子是快速排序(quicksort)。不幸的是,PHP并不擅长递归。Zeev,一个PHP开发人员,说道:“PHP 4.0(Zend)对密集数据使用了栈方式,而不是使用堆方式。也就是说它能容忍的递归函数的数量限 ......

使用 PHP 处理 XML 配置文件


使用 PHP 处理 XML 配置文件
使用 XML 配置文件轻易地配置 PHP 应用程序和对象
级别: 中级
Vikram Vaswani, 创始人, Melonfire
2007 年 11 月 29 日
XML 为应用程序配置文件提供了一种便捷、易用的表达语言。但有时候将这些信息提取到 PHP 脚本中将会面对一个不小的挑战。这正是 XJConf for PHP 包出现的原因:它提 ......

【PHP系列教程】(3)——PHP的类型

PHP支持8种原始类型,其中包括:
4种标量类型:boolean(布尔型)、integer(整型)、float(浮点型)、string(字符型);
2种复合类型:array(数组)、object(对象);
2种特殊类型:resource(资源)、NULL
您可能还会读到一些关于“双精度(double)”类型的参考。实际上 double 和 float 是相同的,由于一些历史的原 ......

php获取xml属性值

<!-- 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 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号