PHP cookbook, 2nd editon, Recipe 12.5
来源:互联网 发布:2015中国经济数据 编辑:程序博客网 时间:2024/05/16 13:00
$reader = new XMLReader();
$reader->XML($catalog);
// Perl Cookbook ISBN is 0596003137
// Use array to make it easy to add additional ISBNs
$isbns = array('0596003137' => true);
/* Loop through document to find first <book> */
while ($reader->read()) {
/* If you're at an element named 'book' */
if ($reader->nodeType == XMLREADER::ELEMENT &&
$reader->localName == 'book') {
break;
}
}
/* Loop through <book>s to find right ISBNs */
do {
if ($reader->moveToAttribute('isbn') &&
isset($isbns[$reader->value])) {
while ($reader->read()) {
switch ($reader->nodeType) {
case XMLREADER::ELEMENT:
print $reader->localName . ": ";
break;
case XMLREADER::TEXT:
print $reader->value . " ";
break;
case XMLREADER::END_ELEMENT;
if ($reader->localName == 'book') {
break 2;
}
}
}
}
} while ($reader->next());
?>
The first while( ) iterates sequentially until it finds the first <book> element.
Having lined yourself up correctly, you then break out of the loop and start checking ISBN numbers. That's handled inside a do... while( ) loop that uses $reader->next( ) to move down the <book> list. You cannot use a regular while( ) here or you'll skip over the first <book>. Also, this is a perfect example of when to use $reader->next( ) instead of $reader->read( ).
If the ISBN matches a value in $isbns, then you want to process the data inside the current <book>. This is handled using yet another while( ) and a switch( ).
There are three different switch( ) cases: an opening element, element text, and a closing element. If you're opening an element, you print out the element's name and a colon. If you're text, you print out the textual data. And if you're closing an element, you check to see whether you're closing the <book>. If so, then you've reached the end of the data for that particular book, and you need to return to the do... while( ) loop. This is handled using a break 2;; while jumps back two levels, instead of the usual one level.
- PHP cookbook, 2nd editon, Recipe 12.5
- PHP Developer's Cookbook (2nd Edition)
- 正在学习“Learning Python 2nd Editon”
- Access Cookbook, 2nd Edition
- Active Directory Cookbook, 2nd Edition
- Active Directory Cookbook, 2nd Edition
- CSS Cookbook, 2nd Edition [ILLUSTRATED]
- OReilly Cisco IOS Cookbook 2nd Edition Dec 2006
- unity-Unity 5.x Shaders and Effects Cookbook(2nd)
- Paleo Recipe Book - Brand New Paleo Cookbook
- PHP Essentials, 2nd Edition
- programming php,2nd Edition
- PHP Cookbook
- C# Cookbook, 2nd Edition 学习笔记(第一章1-10节)
- bash Cookbook, 2nd Edition Solutions and Examples for bash Users pdf
- OReilly.bash.Cookbook.2nd.Edition.2017.10.pdf 英文原版 免费下载
- Multithreading with C# Cookbook, 2nd Edition.pdf 英文原版 免费下载
- Visual Studio 2015 Cookbook, 2nd Edition.pdf 英文原版 免费下载
- 逐步讲解用C#实现俄罗斯方块之核心代码[中]
- HTTP协议基础
- .NET Remoting技术连载(一)
- 将Rubyist.cn进行了初步美化
- 第一贴
- PHP cookbook, 2nd editon, Recipe 12.5
- 电脑死机的原因
- 所见即所得的开源Flex设计工具:flexible
- 连续几天了,都没有写什么咚咚了;
- 系统实施工程师主要工作职则
- 记我的朋友
- 很高兴来到这里
- SOA基本概念
- STL中map用法详解