易截截图软件、单文件、免安装、纯绿色、仅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 读取和编写 XML DOM


用 PHP 读取和编写 XML DOM
使用 DOM 库、SAX 解析器和正则表达式
文档选项
打印本页
将此页作为电子邮件发送
级别: 中级
Jack Herrington (jack_d_herrington@codegeneration.net), 高级软件工程师, "Code Generation Network"
2006 年 2 月 06 日
有许多技术可用于用 PHP 读取和编写 XML。本文提供了三种 ......

使用 PHP 处理 XML 配置文件


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

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号