WEB开发网
开发学院WEB开发PHP 面向PHP开发人员的XML 第3部分: 读、操纵和写XML的... 阅读

面向PHP开发人员的XML 第3部分: 读、操纵和写XML的高级技术

 2008-11-19 22:26:04 来源:WEB开发网   
核心提示: 使用 Simplexml(标准化越来越近了) 操纵 xml(标准化越来越近了)Simplexml(标准化越来越近了) 扩展适合于操纵不很复杂或者嵌套不太深并且没有混合内容的 xml(标准化越来越近了) 文档,Simplexml(标准化越来越近了) 比 DOM 更容易编码,面向PHP开发人员的XML 第3部分: 读、操

使用 Simplexml(标准化越来越近了) 操纵 xml(标准化越来越近了)

Simplexml(标准化越来越近了) 扩展适合于操纵不很复杂或者嵌套不太深并且没有混合内容的 xml(标准化越来越近了) 文档。Simplexml(标准化越来越近了) 比 DOM 更容易编码,就像名称所暗示的那样。如果处理的文档结构已知,就更加直观。libxml(标准化越来越近了)2 架构的互操作性大大增强了 DOM 和 Simplexml(标准化越来越近了) 的灵活性,能够随意在 DOM 和 Simplexml(标准化越来越近了) 之间交换导入格式。

使用 Simplexml(标准化越来越近了)

使用 Simplexml(标准化越来越近了) 操纵文档非常简单,编码也很快捷。下面的代码使用 Simplexml(标准化越来越近了) 扩展解析查询结果。如您所料,下面的 Simplexml(标准化越来越近了) 代码(参见 清单 2)比上面 清单 1 所示的 DOM 代码示例更紧凑。

清单 2. Yahoo Simplexml(标准化越来越近了) 例子

<?php
//This query does a search for any Web pages relevant to "xml(标准化越来越近了) Query"
$query = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?".
"query=%5C%22xml(标准化越来越近了)%20Query%5C%22&appid=YahooDemo";
$xml(标准化越来越近了) = simplexml(标准化越来越近了)_load_file($query);
// Load up the root element attributes
foreach($xml(标准化越来越近了)->attributes() as $name=>$attr) {
$res[$name]=$attr;
}
//Use one of those "informational" elements to display the total
//number of results for the query.
echo "<p>The query returns ".$res["totalResultsAvailable"].
" total results The first 10 are as follows:</p>";
//Unlike with DOM, where we loaded the entire document into the
//result object, with Simplexml(标准化越来越近了), we get back an object in the
//first place, so we can just use the number of results returned
//to loop through the Result members.
for($i=0; $i<$res['totalResultsReturned']; $i++) {
//The object represents each piece of data as a member variable
//rather than an array element, so the syntax is a little bit
//different from the DOM version.
$thisResult = $xml(标准化越来越近了)->Result[$i];
echo "<a href='".$thisResult->ClickUrl."'><b>".
$thisResult->Title."</b></a>: ";
echo $thisResult->Summary;
echo "<br /><br />";
}
?>

上一页  1 2 3 4 5  下一页

Tags:面向 PHP 开发

编辑录入:coldstar [复制链接] [打 印]
赞助商链接