燕之庐网站建设 - 优质网站设计公司

Posts Tagged ‘xml’

php

2009/11/22

巧用PHP文本模板读取编写XML DOM

Tags: , ,

有许多技术可用于用 PHP 读取和编写 XML。本文提供了三种方法读取 XML:使用 DOM 库、使用 SAX 解析器和使用正则表达式。还介绍了使用 DOM 和 PHP 文本模板编写 XML。

用 PHP 读取和编写可扩展标记语言(XML)看起来可能有点恐怖。实际上,XML 和它的所有相关技术可能是恐怖的,但是用 PHP 读取和编写 XML 不一定是项恐怖的任务。首先,需要学习一点关于 XML 的知识 —— 它是什么,用它做什么。然后,需要学习如何用 PHP 读取和编写 XML,而有许多种方式可以做这件事。

本文提供了 XML 的简短入门,然后解释如何用 PHP 读取和编写 XML。

什么是 XML?

XML 是一种数据存储格式。它没有定义保存什么数据,也没有定义数据的格式。XML 只是定义了标记和这些标记的属性。格式良好的 XML 标记看起来像这样:

<name>Jack Herrington</name>

这个 <name> 标记包含一些文本:Jack Herrington。

不包含文本的 XML 标记看起来像这样:

<powerUp />

用 XML 对某件事进行编写的方式不止一种。例如,这个标记形成的输出与前一个标记相同:

<powerUp></powerUp>

也可以向 XML 标记添加属性。例如,这个 <name> 标记包含 first 和 last 属性:

<name first=”Jack” last=”Herrington” />

也可以用 XML 对特殊字符进行编码。例如,& 符号可以像这样编码:

&

包含标记和属性的 XML 文件如果像示例一样格式化,就是格式良好的,这意味着标记是对称的,字符的编码正确。清单 1 是一份格式良好的 XML 的示例。

清单 1. XML 图书列表示例

<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>

清单 1 中的 XML 包含一个图书列表。父标记 <books> 包含一组 <book> 标记,每个 <book> 标记又包含 <author>、<title> 和 <publisher> 标记。

当 XML 文档的标记结构和内容得到外部模式文件的验证后,XML 文档就是正确的。模式文件可以用不同的格式指定。对于本文来说,所需要的只是格式良好的 XML。

如果觉得 XML 看起来很像超文本标记语言(HTML),那么就对了。XML 和 HTML 都是基于标记的语言,它们有许多相似之处。但是,要着重指出的是:虽然 XML 文档可能是格式良好的 HTML,但不是所有的 HTML 文档都是格式良好的 XML。换行标记(br)是 XML 和 HTML 之间区别的一个好例子。这个换行标记是格式良好的 HTML,但不是格式良好的 XML:

<p>This is a paragraph<br>
With a line break</p>

这个换行标记是格式良好的 XML 和 HTML:

<p>This is a paragraph<br />
With a line break</p>

如果要把 HTML 编写成同样是格式良好的 XML,请遵循 W3C 委员会的可扩展超文本标记语言(XHTML)标准(参见参考资料)。所有现代的浏览器都能呈现 XHTML。而且,还可以用 XML 工具读取 XHTML 并找出文档中的数据,这比解析 HTML 容易得多。

使用 DOM 库读取 XML

读取格式良好的 XML 文件最容易的方式是使用编译成某些 PHP 安装的文档对象模型 (DOM)库。DOM 库把整个 XML 文档读入内存,并用节点树表示它,如图 1 所示。

图 1. 图书 XML 的 XML DOM 树

树顶部的 books 节点有两个 book 子标记。在每本书中,有 author、publisher 和 title 几个节点。author、publisher 和 title 节点分别有包含文本的文本子节点。

读取图书 XML 文件并用 DOM 显示内容的代码如清单 2 所示。

清单 2. 用 DOM 读取图书 XML

<?php
$doc = new DOMDocument();
$doc->load( 'books.xml' );

$books = $doc->getElementsByTagName( "book" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$author = $authors->item(0)->nodeValue;

$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;

$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;

echo "$title - $author - $publisher\n";
}
?>

脚本首先创建一个 new DOMdocument 对象,用 load 方法把图书 XML 装入这个对象。之后,脚本用 getElementsByName 方法得到指定名称下的所有元素的列表。

在 book 节点的循环中,脚本用 getElementsByName 方法获得 author、publisher 和 title 标记的 nodeValue。nodeValue 是节点中的文本。脚本然后显示这些值。

可以在命令行上像这样运行 PHP 脚本:

% php e1.php
PHP Hacks – Jack Herrington – O’Reilly
Podcasting Hacks – Jack Herrington – O’Reilly
%

可以看到,每个图书块输出一行。这是一个良好的开始。但是,如果不能访问 XML DOM 库该怎么办?
用 SAX 解析器读取 XML

读取 XML 的另一种方法是使用 XML Simple API(SAX)解析器。PHP 的大多数安装都包含 SAX 解析器。SAX 解析器运行在回调模型上。每次打开或关闭一个标记时,或者每次解析器看到文本时,就用节点或文本的信息回调用户定义的函数。

SAX 解析器的优点是,它是真正轻量级的。解析器不会在内存中长期保持内容,所以可以用于非常巨大的文件。缺点是编写 SAX 解析器回调是件非常麻烦的事。清单 3 显示了使用 SAX 读取图书 XML 文件并显示内容的代码。

清单 3. 用 SAX 解析器读取图书 XML

<?php
$g_books = array();
$g_elem = null;

function startElement( $parser, $name, $attrs )
{
global $g_books, $g_elem;
if ( $name == 'BOOK' ) $g_books []= array();
$g_elem = $name;
}

function endElement( $parser, $name )
{
global $g_elem;
$g_elem = null;
}

function textData( $parser, $text )
{
global $g_books, $g_elem;
if ( $g_elem == 'AUTHOR' ||
$g_elem == 'PUBLISHER' ||
$g_elem == 'TITLE' )
{
$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
}
}

$parser = xml_parser_create();

xml_set_element_handler( $parser, "startElement", "endElement" );
xml_set_character_data_handler( $parser, "textData" );

$f = fopen( 'books.xml', 'r' );

while( $data = fread( $f, 4096 ) )
{
xml_parse( $parser, $data );
}

xml_parser_free( $parser );

foreach( $g_books as $book )
{
echo $book['TITLE']." - ".$book['AUTHOR']." - ";
echo $book['PUBLISHER']."\n";
}
?>

脚本首先设置 g_books 数组,它在内存中容纳所有图书和图书信息,g_elem 变量保存脚本目前正在处理的标记的名称。然后脚本定义回调函数。在这个示例中,回调函数是 startElement、endElement 和 textData。在打开和关闭标记的时候,分别调用 startElement 和 endElement 函数。在开始和结束标记之间的文本上面,调用 textData。

在这个示例中,startElement 标记查找 book 标记,在 book 数组中开始一个新元素。然后,textData 函数查看当前元素,看它是不是 publisher、title 或 author 标记。如果是,函数就把当前文本放入当前图书。

为了让解析继续,脚本用 xml_parser_create 函数创建解析器。然后,设置回调句柄。之后,脚本读取文件并把文件的大块内容发送到解析器。在文件读取之后,xml_parser_free 函数删除解析器。脚本的末尾输出 g_books 数组的内容。

可以看到,这比编写 DOM 的同样功能要困难得多。如果没有 DOM 库也没有 SAX 库该怎么办?还有替代方案么?

用正则表达式解析 XML

可以肯定,即使提到这个方法,有些工程师也会批评我,但是确实可以用正则表达式解析 XML。清单 4 显示了使用 preg_ 函数读取图书文件的示例。

清单 4. 用正则表达式读取 XML

<?php
$xml = "";
$f = fopen( 'books.xml', 'r' );
while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
fclose( $f );

preg_match_all( "/\<book\>(.*?)\<\/book\>/s",
$xml, $bookblocks );

foreach( $bookblocks[1] as $block )
{
preg_match_all( "/\<author\>(.*?)\<\/author\>/",
$block, $author );
preg_match_all( "/\<title\>(.*?)\<\/title\>/",
$block, $title );
preg_match_all( "/\<publisher\>(.*?)\<\/publisher\>/",
$block, $publisher );
echo( $title[1][0]." - ".$author[1][0]." - ".
$publisher[1][0]."\n" );
}
?>

请注意这个代码有多短。开始时,它把文件读进一个大的字符串。然后用一个 regex 函数读取每个图书项目。最后用 foreach 循环,在每个图书块间循环,并提取出 author、title 和 publisher。

那么,缺陷在哪呢?使用正则表达式代码读取 XML 的问题是,它并没先进行检查,确保 XML 的格式良好。这意味着在读取之前,无法知道 XML 是否格式良好。而且,有些格式正确的 XML 可能与正则表达式不匹配,所以日后必须修改它们。

我从不建议使用正则表达式读取 XML,但是有时它是兼容性最好的方式,因为正则表达式函数总是可用的。不要用正则表达式读取直接来自用户的 XML,因为无法控制这类 XML 的格式或结构。应当一直用 DOM 库或 SAX 解析器读取来自用户的 XML。

用 DOM 编写 XML

读取 XML 只是公式的一部分。该怎样编写 XML 呢?编写 XML 最好的方式就是用 DOM。清单 5 显示了 DOM 构建图书 XML 文件的方式。

清单 5. 用 DOM 编写图书 XML

<?php
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);

$doc = new DOMDocument();
$doc->formatOutput = true;

$r = $doc->createElement( "books" );
$doc->appendChild( $r );

foreach( $books as $book )
{
$b = $doc->createElement( "book" );

$author = $doc->createElement( "author" );
$author->appendChild(
$doc->createTextNode( $book['author'] )
);
$b->appendChild( $author );

$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $book['title'] )
);
$b->appendChild( $title );

$publisher = $doc->createElement( "publisher" );
$publisher->appendChild(
$doc->createTextNode( $book['publisher'] )
);
$b->appendChild( $publisher );

$r->appendChild( $b );
}

echo $doc->saveXML();
?>

在脚本的顶部,用一些示例图书装入了 books 数组。这个数据可以来自用户也可以来自数据库

示例图书装入之后,脚本创建一个 new DOMDocument,并把根节点 books 添加到它。然后脚本为每本书的 author、title 和 publisher 创建节点,并为每个节点添加文本节点。每个 book 节点的最后一步是重新把它添加到根节点 books。

脚本的末尾用 saveXML 方法把 XML 输出到控制台。(也可以用 save 方法创建一个 XML 文件。)脚本的输出如清单 6 所示。

清单 6. DOM 构建脚本的输出

% php e4.php
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
%

使用 DOM 的真正价值在于它创建的 XML 总是格式正确的。但是如果不能用 DOM 创建 XML 时该怎么办?

用 PHP 编写 XML

如果 DOM 不可用,可以用 PHP 的文本模板编写 XML。清单 7 显示了 PHP 如何构建图书 XML 文件。

清单 7. 用 PHP 编写图书 XML

<?php
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
?>
<books>
<?php

foreach( $books as $book )
{
?>
<book>
<title><?php echo( $book['title'] ); ?></title>
<author><?php echo( $book['author'] ); ?>
</author>
<publisher><?php echo( $book['publisher'] ); ?>
</publisher>
</book>
<?php
}
?>
</books>

脚本的顶部与 DOM 脚本类似。脚本的底部打开 books 标记,然后在每个图书中迭代,创建 book 标记和所有的内部 title、author 和 publisher 标记。

这种方法的问题是对实体进行编码。为了确保实体编码正确,必须在每个项目上调用 htmlentities 函数,如清单 8 所示。

清单 8. 使用 htmlentities 函数对实体编码

<books>
<?php

foreach( $books as $book )
{
$title = htmlentities( $book['title'], ENT_QUOTES );
$author = htmlentities( $book['author'], ENT_QUOTES );
$publisher = htmlentities( $book['publisher'], ENT_QUOTES );
?>
<book>
<title><?php echo( $title ); ?></title>
<author><?php echo( $author ); ?> </author>
<publisher><?php echo( $publisher ); ?>
</publisher>
</book>
<?php
}
?>
</books>

这就是用基本的 PHP 编写 XML 的烦人之处。您以为自己创建了完美的 XML,但是在试图使用数据的时候,马上就会发现某些元素的编码不正确。

结束语

XML 周围总有许多夸大之处和混淆之处。但是,并不像您想像的那么难 —— 特别是在 PHP 这样优秀的语言中。在理解并正确地实现了 XML 之后,就会发现有许多强大的工具可以使用。XPath 和 XSLT 就是这样两个值得研究的工具。

php

2009/11/19

在PHP中开发XML应用程序之基础篇

Tags: , ,

一、 XML简介

XML(可扩展的标注语言)是一种W3C标准,主要用于Web应用程序和服务器之间实现容易的交互、数据的存储与使用。

使用XML标准编码的数据具有能容易被人和计算机解释的意义和结构。XML数据是平台和应用程序独立的。不用多说,这本身就使XML成为适合于互联网的一个理想的数据交换格式(事实上,它正是因这一用途而被开发的)。最近,宽带连接的增长及消费者对于越过任何媒体进行数据共享的应用软件的需求意味着,XML Web服务和应用软件正变得越来越丰富。

XML的发明正是为了解决描述网上丰富的数据的组织问题;而目前为止,这一问题仅能够通过HTML的巧妙使用得到部分地解决。

下面是一XML文档的实例:

<?xml version=”1.0″?>
<party>
<location>My House</location>
<time>7pm</time>
<guest>
<name>John Bloggs</name>
<item>Crate of Fosters</item>
</guest>
<guest>
<name>Sara Bloggs</name>
<item>Umbrella</item>
</guest>
<guest>
<name>David Fig</name>
<item>Bombay Mix</item>
</guest>
</party>

如果你以前没见过XML,那么你可以认为它看起来象HTML。HTML是一种SGML应用程序,而XML是它的一个子集。然而,其相似性还包括它们具有相似的标注分隔符。

仅需看一下上面的XML片断,我们就能看到,该数据是描述一个具有一些客人的聚会; 其中,每一个客人相应于一项。用于描述数据的标签名完全由作者来选择。所有XML标准要求:数据必须是一致的并且用于描述数据的标签为良构的。我们可以进 一步用一种文档类型声明(DTD)或一个XML模式来强制数据的完整性。然而为简化起见,我们在本文中将仅使用普通的XML。

二、 XML应用程序

刚才,我们已经看到了如何使用XML来描述任何种类的数据。事实上,XML已经在今天的许多Web应用程序中得到广泛使用,下面是一些著名的应用描述:

· XHTML-这是使用最广泛的XML应用程序之一。它类似基于HTML的SGML-用于描述数据在网页上的显示方式。XHTML使用一DTD来确保所有的 文档遵循标准。XHTML的出现使Web程序员的开发稍微容易了一些;然而,一种完全兼容于CSS和XHTML标准的web浏览器尚未出现。

· XML-RPC-远程过程调用(RPC),应用于分布式应用程序中以调用远程计算机上的过程。XML-RPC使用XML对关于过程调用的信息进行编码,并且使用HTTP把它发送到接收计算机。然后,过程的返回值被再次用XML编码并用HTTP连接发送回调用者计算机。

· RSS-真正简单的聚合/丰富的站点摘要,它是一种用来聚合web站点内容(例如新闻、文章、共享价格和链接等)的方法,它用一个特殊的应用程序(一个聚合器)定期更新用户PC上的RSS回馈。该RSS数据是使用XML进行编码和传输的。

· AJAX-异步的JavaScript和XML,允许web开发者创建具有丰富特征的事件驱动的运行在web浏览器上的web应用程序。其 中,JavaScript用于把XML编码的数据发送到服务器端脚本(或从服务器端接收XML编码的数据),并允许局部的实时的页面更新而不需要更新所有 页面内容。

上面仅仅是XML的可能的应用的一部分。在以后文章中,我们将分析如何在PHP中使用这些应用软件。

三、 在PHP中使用XML

自从PHP 5.0以来,PHP能与XML交互的可用选项显著地增加。而PHP版本4所能提供的是不稳定的而且是非w3c兼容的DOM XML扩展。
下面,我将集中讨论PHP 5所提供给我们的三个允许我们与XML交互的方法:DOM,简单XML和XPath。在可能之处,我将建议最适合于每种方法的条件和数据。所有的示例代码将使用XML数据源来描述一个库及其中包含的书。

<xml version=”1.0″?>
<library>
<categories>
<category cid=”1″>Web Development</category>
<category cid=”2″>Database Programming</category>
<category cid=”3″>PHP</category>
<category cid=”4″>Java</category>
</categories>
<books>
<book>
<title>Apache 2</title>
<author>Peter Wainwright</author>
<publisher>Wrox</publisher>
<category>1</category>
</book>
<book>
<title>Advanced PHP Programming</title>
<author>George Schlossnagle</author>
<publisher>Developer Library</publisher>
<category>1</category>
<category>3</category>
</book>
<book>
<title>Visual FoxPro 6 – Programmers Guide</title>
<author>Eric Stroo</author>
<publisher>Microsoft Press</publisher>
<category>2</category>
</book>
<book>
<title>Mastering Java 2</title>
<author>John Zukowski</author>
<publisher>Sybex</publisher>
<category>4</category>
</book>
</books>
</library>

四、 DOM

DOM PHP扩展名允许使用W3C DOM API在XML文档上进行操作。在PHP 5出现之前,这是PHP能存取XML文档的唯一方法。如果你在JavaScript中使用了DOM,那么会认识到这些对象模型几乎是一样的。

由于DOM方法在遍历和操作XML文档时比较罗嗦,所以任何DOM兼容的代码都有明显的优点-与任何其它实现相同的W3C兼容的对象模型的API兼容。

在下面的实例代码中,我们使用DOM来显示关于每本书的信息。首先,我们遍历一下列表目录,把它们的Id和相应的名字装载到一个索引数组中。然后,我们显示每本书的一个简短描述:

PHP:

<?php
/*这里我们必须指定XML版本:也即是1.0 */
$xml = new DomDocument(’1.0′);
$xml->load(’xml/library.xml’);
/*首先,创建一个目录列表*/
$categories = array();
$XMLCategories = $xml->getElementsByTagName(’categories’)->item(0);
foreach($XMLCategories->getElementsByTagName(’category’) as $categoryNode) {
/*注意我们是如何得到属性的*/
$cid = $categoryNode->getAttribute(’cid’);
$categories[$cid] = $categoryNode->firstChild->nodeValue;
}
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?
php foreach($xml->getElementsBytagName(’book’) as $book):
/*查找标题*/
$title = $book->getElementsByTagName(’title’)->item(0)->firstChild->nodeValue;
/*查找作者-为了简化起见,我们假设仅仅有一个作者*/
$author = $book->getElementsByTagName(’author’)->item(0)->firstChild->nodeValue;
/* 列表目录*/
$bookCategories = $book->getElementsByTagName(’category’);
$catList = ”;
foreach($bookCategories as $category) {
$catList .= $categories[$category->firstChild->nodeValue] . ‘, ‘;
}
$catList = substr($catList, 0, -2); ?>
<div>
<h2><?php echo($title) ?></h2>
<p><b>Author:</b>: <?php echo($author) ?></p>
<p><b>Categories: </b>: <?php echo($catList) ?></p>
</div>
<? php endforeach; ?>
</html>

再提一下,修改XML是较麻烦的。例如,添加一个目录的代码如下:

PHP:

function addCategory(DOMDocument $xml, $catID, $catName) {
$catName = $xml->createTextNode($catName); //创建一个结点以存储文本
$category = $xml->createElement(’category’); //创建一个目录元素
$category->appendChild($catName); //把文本添加到目录元素上
$category->setAttribute(’cid’, $catID); //设置目录的ID
$XMLCategories = $xml->getElementsByTagName(’categories’)->item(0);
$XMLCategories->appendChild($category); //添加新目录
}

五、 保存XML

你可以使用save()和saveXML()方法之一来把DOM描述转换回XML字符串描述。save()方法用一指定的命名把XML保存到一个文件中,而saveXML()从文档的部分或整体中返回一个字符串。

$xml->save(’xml/library.xml’);
//保存全部文件
$categories=$xml->saveXML($XMLCategories);
//返回一个包含种类的字符串

为了说明把DOM兼容的代码移植到另外的语言是如何容易,下面是用JavaScript形式实现的与以上功能相同的代码:

Javascript:
function doXML(){
/* 首先创建一个种类列表*/
var categories = Array();
var XMLCategories = xml.getElementsByTagName(’categories’)[0];
var theCategories = XMLCategories.getElementsByTagName(’category’);
for (var i = 0; i < theCategories.length; i++) {
/* 注意我们是怎样得到属性的*/
var cid = theCategories[i].getAttribute(’cid’);
categories[cid] = theCategories[i].firstChild.nodeValue;
}
var theBooks = xml.getElementsByTagName(’book’);
for(var i = 0; i < theBooks.length; i++) {
var book = theBooks[i];
/* 查找标题*/
var title = book.getElementsByTagName(’title’)[0].firstChild.nodeValue;
/* 查找作者-为简单起见,我们假定仅有一个作者*/
var author = book.getElementsByTagName(’author’)[0].firstChild.nodeValue;
/* 列出种类*/
var bookCategories = book.getElementsByTagName(’category’);
var catList = ”;
for(var j = 0; j < bookCategories.length; j++) {
catList += categories[bookCategories[j].firstChild.nodeValue] + ‘, ‘;
}
catList = catList.substring(0, catList.length -2);
document.open();
document.write(”<h2>” + title + “</h2>”);
document.write(”<p><b>Author:</b>: ” + author + “</p>”);
document.write(”<p><b>Categories: </b>: ” + catList + “</p>”);
}
document.close();
}

六、 简单XML

简单XML确实简单。它允许使用对象和数组存取方法来存取一个XML文档及其元素和属性。操作方式很简单:

· 元素(Element)-这些被描述为SimpleXMLElement对象的单个属性。当有多个作为文档或元素的子元素存在时,每个元素能被使用数组索引标志加以存取。

$xml->books;//返回元素”books”
$xml->books->book[0];//返回在books元素中的第一本书

· 属性(Attribute)-元素的属性是通过关联数组标志来存取和设置的,此时每一个索引对应于一个属性名。

$category['cid'];//返回cid属性的值

· 元素数据(Element Data)-为了检索包含在一个元素内的文本数据,必须使用(string)显式地把它被转换为一个字符串或使用print或echo输出它。如果一个元素包含多个文本结点,那么它们将按被找到的顺序连接起来。

echo ($xml->books->book[0]->title);//显示第一本书的标题

下面是使用简单XML进行转换的原来的实例。为了装载XML文件,我们使用simplexml_load_file()函数,由它来分析该XML文件并且把它装载进一个SimpleXMLElement对象中:

PHP:

<?php
$xml = simplexml_load_file(’xml/library.xml’);
/* 把一个列表的目录装载到一个数组中*/
$categories = array();
foreach($xml->categories->category as $category) {
$categories[(string) $category['cid']] = (string) $category;
}
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?php foreach($xml->books->book as $book):
/* 列举目录*/
$catList = ”;
foreach($book->category as $category) {
$catList .= $categories[((string) $category)] . ‘, ‘;
}
$catList = substr($catList, 0, -2); ?>
<div>
<h2><?php echo($book->title) ?></h2>
<p><b>Author:</b>: <?php echo($book->author) ?></p>
<p><b>Categories: </b>: <? php echo($catList) ?></p>
</div>
<? php endforeach; ?>
</html>

七、 修改XML

尽管文本数据和属性值可以通过使用简单XML加以设置,但是不能新建这些对象。然而,SimpleXM的确提供了一种方法来实现DomElement对 象和DomElement对象之间的转换。为此,我修改了addCategory()函数来说明如何使用simplexml_import_dom()函 数以添加目录和把该文档转换回简单的XML格式:

PHP:

function addCategory(SimpleXMLElement &$sXML, $catID, $catName) {
$xml = new DOMDocument;
$xml->loadXML($sXML->asXML());
$catName = $xml->createTextNode($catName); //创建一个结点来存放该文本
$category = $xml->createElement(’category’); //创建一个目录元素
$category->appendChild($catName); //把文本添加到目录元素
$category->setAttribute(’cid’, $catID); //设置目录id
$XMLCategories = $xml->getElementsByTagName(’categories’)->item(0);
$XMLCategories->appendChild($category); //添加新目录
$sXML = simplexml_import_dom($xml);
return $sXML;
}

同样,SimpleXMLElement对象的asXML()函数可以用来检索XML字符串并把它保存回一个文件中。

八、 xPath

毫无疑问,Xpath是”XML蛋糕之上的樱桃”。XPath允许你使用象SQL一样的查询来查找一个XML文档中的特定信息。DOM和SimpleXML都有内置的对XPath的支持,如SQL,可以被用来提取你想从一XML文档中提取的任何内容。

· //category-查找所有的在文档中出现的任何category。

· /library/books-查找所有作为library的孩子出现的books

· /library/categories/category[@cid]-查找所有作为library/categories的孩子出现且属性为cid的category。

· /library/categories/category[@att='2']-查找所有作为library/categories的孩子且具有属性cid的值为2出现的category。

· /library/books/book[title='Apache 2']-查找所有作为/library/books的孩子且其标题元素有一个值为Apache 2出现的book。

其实,这仅是xPath冰山之一角。你可以使用xPath来创建大量复杂的查询以便从你的文档中提取几乎任何信息。我再次修改了示例代码来向你展示使用xPath是多么轻松愉快的事情。

PHP:

<?php
$xml = simplexml_load_file(’xml/library.xml’);
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?php foreach(((array)$xml->xpath(”/library/books/book”)) as $book):
/*列表目录*/
$catList = ”;
foreach($book->category as $category) {
/*得到具有这个ID的目录*/
$category = $xml->xpath(”/library/categories/category[@cid='$category']“);
$catList .= (string) $category[0] . ‘, ‘;
}
$catList = substr($catList, 0, -2); ?>
<div>
<h2><?php echo($book->title) ?></h2>
<p><b>Author:</b>: <?php echo($book->author) ?></p>
<p><b>Categories: </b>: <?php echo($catList) ?></p>
</div>
<?php endforeach; ?>
</html>

九、 DOM和XPath

在DOM中计算XPath查询需要创建一个DOMXPath对象,下面的evaluate()函数返回一个DOMElement数组。

$xPath = new DOMXPath($xml);
$xPath->evaluate(”/library/books/book[title='Apache 2']“);

十、 结论

现在,我们学习了如何使用了PHP提供给我们的工具来与XML交互。至此,我们已经被”武装起来”并准备好深入钻研XML应用程序了。在下一篇文章中,我们将讨论AJAX及其如何应用于象Google这样的站点开发的。

php

2009/11/14

使用PHP 5.0 轻松解析XML文档(6)

Tags: ,

下面是通过createNode()函数,添加商品, 然后显示添加后的结果

<?xml version="1.0" encoding="GB2312" ?>
<shop>
 <name>华联</name>
 <address>北京长安街-9999号</address>
 <desc>连锁超市</desc>
 <telphone>123456789</telphone>
 <cat id="food">
  <goods id="food11">
   <name>food11</name>
   <price>12.90</price>
   <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword>
  </goods>
  <goods id="food12">
   <name>new food12</name>
   <price>22.10</price>
  </goods>
  <goods id="food13">
   <name>food13</name>
   <price>100</price>
  </goods>
 </cat>
 <cat>
  <goods id="tel21">
   <name>tel21</name>
   <price>1290</price>
  </goods>
 </cat>
 <cat id="coat">
  <goods id="coat31">
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id="coat32">
   <name>coat32</name>
   <price>45</price>
  </goods>
 </cat>
 <special id="hot">
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
 </special>
</shop>

下面是通过removeNode()函数,删除商品, 然后显示删除后的结果

<?xml version="1.0" encoding="GB2312" ?>
<shop>
 <name>华联</name>
 <address>北京长安街-9999号</address>
 <desc>连锁超市</desc>
 <telphone>123456789</telphone>
 <cat id="food">
  <goods id="food11">
   <name>food11</name>
   <price>12.90</price>
   <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword>
  </goods>
  <goods id="food13">
   <name>food13</name>
   <price>100</price>
  </goods>
 </cat>
 <cat>
  <goods id="tel21">
   <name>tel21</name>
   <price>1290</price>
  </goods>
 </cat>
 <cat id="coat">
  <goods id="coat31">
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id="coat32">
   <name>coat32</name>
   <price>45</price>
  </goods>
 </cat>
 <special id="hot">
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
 </special>
</shop>

php

使用PHP 5.0 轻松解析XML文档(5)

Tags: ,

下面是通过getNode()函数,返回某一个分类下的所有商品的信息商品名:

food11
Array
(
    [name] => food11
    [price] => 12.90
)
Array
(
    [id] => food11
)
商品名:food12
Array
(
    [name] => food12
    [price] => 22.10
    [desc] => Array
        (
            [value] => 好东西推荐
            [attrs] => Array
                (
                    [creator] => hahawen
                )
        )
)
Array
(
    [id] => food12
)
  下面是通过findNodeByPath()函数,返回某一商品的信息商品名:
food11

Array
(
    [name] => food11
    [price] => 12.90
)
Array
(
    [id] => food11
)

下面是通过setValue()函数,给商品”food11″添加属性, 然后显示添加后的结果

<?xml version="1.0" encoding="GB2312" ?>
<shop>
 <name>华联</name>
 <address>北京长安街-9999号</address>
 <desc>连锁超市</desc>
 <telphone>123456789</telphone>
 <cat id="food">
  <goods id="food11">
   <name>food11</name>
   <price>12.90</price>
   <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword>
  </goods>
  <goods id="food12">
   <name>food12</name>
   <price>22.10</price>
   <desc creator="hahawen">好东西推荐</desc>
  </goods>
 </cat>
 <cat>
  <goods id="tel21">
   <name>tel21</name>
   <price>1290</price>
  </goods>
 </cat>
 <cat id="coat">
  <goods id="coat31">
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id="coat32">
   <name>coat32</name>
   <price>45</price>
  </goods>
 </cat>
 <special id="hot">
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
 </special>
</shop>

下面是通过removeValue()/removeAttribute()函数,给商品”food11″改变和删除属性, 然后显示操作后的结果

<?xml version="1.0" encoding="GB2312" ?>
<shop>
 <name>华联</name>
 <address>北京长安街-9999号</address>
 <desc>连锁超市</desc>
 <telphone>123456789</telphone>
 <cat id="food">
  <goods id="food11">
   <name>food11</name>
   <price>12.90</price>
   <leaveword author="hahawen" date="2004-12-05">这个商品不错</leaveword>
  </goods>
  <goods id="food12">
   <name>new food12</name>
   <price>22.10</price>
  </goods>
 </cat>
 <cat>
  <goods id="tel21">
   <name>tel21</name>
   <price>1290</price>
  </goods>
 </cat>
 <cat id="coat">
  <goods id="coat31">
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id="coat32">
   <name>coat32</name>
   <price>45</price>
  </goods>
 </cat>
 <special id="hot">
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
 </special>
</shop>

php

2009/11/13

使用PHP 5.0 轻松解析XML文档(4)

Tags: ,

下面是例子运行对结果:

下面是通过函数getSaveData()返回的整个xml数据的数组

Array
(
    [name] => 华联
    [address] => 北京长安街-9999号
    [desc] => 连锁超市
    [cat_food] => Array
        (
            [attrs] => Array
                (
                    [id] => food
                )
            [goods_food11] => Array
                (
                    [name] => food11
                    [price] => 12.90
                    [attrs] => Array
                        (
                            [id] => food11
                        )
                )
            [goods_food12] => Array
                (
                    [name] => food12
                    [price] => 22.10
                    [desc] => Array
                        (
                            [value] => 好东西推荐
                            [attrs] => Array
                                (
                                    [creator] => hahawen
                                )
                        )
                    [attrs] => Array
                        (
                            [id] => food12
                        )
                )
        )
    [cat_1] => Array
        (
            [goods_tel21] => Array
                (
                    [name] => tel21
                    [price] => 1290
                    [attrs] => Array
                        (
                            [id] => tel21
                        )
                )
        )
    [cat_coat] => Array
        (
            [attrs] => Array
                (
                    [id] => coat
                )
            [goods_coat31] => Array
                (
                    [name] => coat31
                    [price] => 112
                    [attrs] => Array
                        (
                            [id] => coat31
                        )
                )
            [goods_coat32] => Array
                (
                    [name] => coat32
                    [price] => 45
                    [attrs] => Array
                        (
                            [id] => coat32
                        )
                )
        )
    [special_hot] => Array
        (
            [attrs] => Array
                (
                    [id] => hot
                )
            [goods_0] => Array
                (
                    [name] => hot41
                    [price] => 99
                )
        )
)

下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容

<?xml version="1.0" encoding="GB2312" ?>
<shop>
 <name>华联</name>
 <address>北京长安街-9999号</address>
 <desc>连锁超市</desc>
 <telphone>123456789</telphone>
 <cat id="food">
  <goods id="food11">
   <name>food11</name>
   <price>12.90</price>
  </goods>
  <goods id="food12">
   <name>food12</name>
   <price>22.10</price>
   <desc creator="hahawen">好东西推荐</desc>
  </goods>
 </cat>
 <cat>
  <goods id="tel21">
   <name>tel21</name>
   <price>1290</price>
  </goods>
 </cat>
 <cat id="coat">
  <goods id="coat31">
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id="coat32">
   <name>coat32</name>
   <price>45</price>
  </goods>
 </cat>
 <special id="hot">
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
 </special>
</shop>

php

使用PHP 5.0 轻松解析XML文档(3)

Tags: ,

文件:SimpleDocumentRoot.php

<?php
/**
 *=========================================================
 *
 * @author     hahawen(大龄青年)
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *=========================================================
 */
/**
 * class SimpleDocumentRoot
 * xml root class, include values/attributes/subnodes.
 * all this pachage's is work for xml file, and method is action as DOM.
 *
 * @package SmartWeb.common.xml
 * @version 1.0
 */
class SimpleDocumentRoot extends SimpleDocumentBase
{
 private $prefixStr = '<?xml version="1.0" encoding="utf-8" ?>';
 private $nodeLists = array();
 function __construct($nodeTag)
 {
        parent::__construct($nodeTag);
 }
    public function createNodeObject($pNodeId, $name, $attributes)
    {
     $seq = sizeof($this->nodeLists);
     $tmpObject = new SimpleDocumentNode($this, $pNodeId, $name, $seq);
     $tmpObject->setAttributes($attributes);
     $this->nodeLists[$seq] = $tmpObject;
     return $tmpObject;
    }
    public function removeNodeById($id)
    {
        if(sizeof($this->nodeLists)==1)
            $this->nodeLists = array();
     else
      unset($this->nodeLists[$id]);
    }
    public function getNodeById($id)
    {
     return $this->nodeLists[$id];
    }
    public function createNode($name, $attributes)
    {
        return $this->createNodeByName($this, $name, $attributes, -1);
    }
    public function removeNode($name)
    {
        return $this->removeNodeByName($this, $name);
    }
    public function getNode($name=null)
    {
        return $this->getNodeByName($this, $name);
    }
    public function getSaveXml()
    {
     $prefixSpace = "";
     $str = $this->prefixStr."\r\n";
     return $str.parent::getSaveXml(0);
    }
}
?>

文件:SimpleDocumentNode.php

<?php
/**
 *=========================================================
 *
 * @author     hahawen(大龄青年)
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *=========================================================
 */
/**
 * class SimpleDocumentNode
 * xml Node class, include values/attributes/subnodes.
 * all this pachage's is work for xml file, and method is action as DOM.
 *
 * @package SmartWeb.common.xml
 * @version 1.0
 */
class SimpleDocumentNode extends SimpleDocumentBase
{
 private $seq = null;
 private $rootObject = null;
    private $pNodeId = null;
    function __construct($rootObject, $pNodeId, $nodeTag, $seq)
    {
     parent::__construct($nodeTag);
        $this->rootObject = $rootObject;
        $this->pNodeId = $pNodeId;
        $this->seq = $seq;
    }
    public function getPNodeObject()
    {
     return ($this->pNodeId==-1)? $this->rootObject: $this->rootObject->getNodeById($this->pNodeId);
    }
    public function getSeq(){
     return $this->seq;
    }
    public function createNode($name, $attributes)
    {
        return $this->createNodeByName($this->rootObject, $name, $attributes, $this->getSeq());
    }
    public function removeNode($name)
    {
        return $this->removeNodeByName($this->rootObject, $name);
    }
    public function getNode($name=null)
    {
        return $this->getNodeByName($this->rootObject, $name);
    }
}
?>

php

使用PHP 5.0 轻松解析XML文档(2)

Tags: ,

文件:SimpleDocumentParser.php

<?php
/**
 *=========================================================
 *
 * @author     hahawen(大龄青年)
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *=========================================================
 */
/**
 * class SimpleDocumentParser
 * use SAX parse xml file, and build SimpleDocumentObject
 * all this pachage's is work for xml file, and method is action as DOM.
 *
 * @package SmartWeb.common.xml
 * @version 1.0
 */
class SimpleDocumentParser
{
 private $domRootObject = null;
 private $currentNO = null;
 private $currentName = null;
 private $currentValue = null;
 private $currentAttribute = null;
 public function getSimpleDocument()
 {
     return $this->domRootObject;
 }
 public function parse($file)
 {
        $xmlParser = xml_parser_create();
     xml_parser_set_option($xmlParser,XML_OPTION_CASE_FOLDING, 0);
     xml_parser_set_option($xmlParser,XML_OPTION_SKIP_WHITE, 1);
     xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
     xml_set_object($xmlParser, $this);
     xml_set_element_handler($xmlParser, "startElement", "endElement");
     xml_set_character_data_handler($xmlParser, "characterData");
        if (!xml_parse($xmlParser, file_get_contents($file)))
            die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlParser)),
        xml_get_current_line_number($xmlParser)));
     xml_parser_free($xmlParser);
 }
 private function startElement($parser, $name, $attrs)
 {
        $this->currentName = $name;
        $this->currentAttribute = $attrs;
        if($this->currentNO == null)
        {
         $this->domRootObject = new SimpleDocumentRoot($name);
         $this->currentNO = $this->domRootObject;
        }
        else
        {
         $this->currentNO = $this->currentNO->createNode($name, $attrs);
        }
 }
    private function endElement($parser, $name)
    {
        if($this->currentName==$name)
        {
         $tag = $this->currentNO->getSeq();
         $this->currentNO = $this->currentNO->getPNodeObject();
            if($this->currentAttribute!=null && sizeof($this->currentAttribute)>0)
          $this->currentNO->setValue($name, array('value'=>$this->currentValue,
       'attrs'=>$this->currentAttribute));
         else
                $this->currentNO->setValue($name, $this->currentValue);
            $this->currentNO->removeNode($tag);
        }
        else
        {
      $this->currentNO = (is_a($this->currentNO, 'SimpleDocumentRoot'))? null:
        $this->currentNO->getPNodeObject();
        }
 }
 private function characterData($parser, $data)
 {
        $this->currentValue = iconv('UTF-8', 'GB2312', $data);
 }
 function __destruct()
 {
     unset($this->domRootObject);
 }
}
?>

文件:SimpleDocumentBase.php

<?php
/**
 *=========================================================
 *
 * @author     hahawen(大龄青年)
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *=========================================================
 */
/**
 * abstract class SimpleDocumentBase
 * base class for xml file parse
 * all this pachage's is work for xml file, and method is action as DOM.
 *
 * 1\ add/update/remove data of xml file.
 * 2\ explode data to array.
 * 3\ rebuild xml file
 *
 * @package SmartWeb.common.xml
 * @abstract
 * @version 1.0
 */
abstract class SimpleDocumentBase
{
 private $nodeTag = null;
 private $attributes = array();
 private $values = array();
 private $nodes = array();
    function __construct($nodeTag)
    {
        $this->nodeTag = $nodeTag;
    }
    public function getNodeTag()
    {
     return $this->nodeTag;
    }
    public function setValues($values){
     $this->values = $values;
    }
    public function setValue($name, $value)
    {
     $this->values[$name] = $value;
    }
    public function getValue($name=null)
    {
     return $name==null? $this->values: $this->values[$name];
    }
    public function removeValue($name)
    {
     unset($this->values["$name"]);
    }
    public function setAttributes($attributes){
        $this->attributes = $attributes;
    }
    public function setAttribute($name, $value)
    {
     $this->attributes[$name] = $value;
    }
    public function getAttribute($name=null)
    {
        return $name==null? $this->attributes: $this->attributes[$name];
    }
    public function removeAttribute($name)
    {
     unset($this->attributes["$name"]);
    }
    public function getNodesSize()
    {
        return sizeof($this->nodes);
    }
    protected function setNode($name, $nodeId)
    {
     $this->nodes[$name] = $nodeId;
    }
    public abstract function createNode($name, $attributes);
    public abstract function removeNode($name);
    public abstract function getNode($name=null);
    protected function getNodeId($name=null)
    {
     return $name==null? $this->nodes: $this->nodes[$name];
    }
    protected function createNodeByName($rootNodeObj, $name, $attributes, $pId)
    {
        $tmpObject = $rootNodeObj->createNodeObject($pId, $name, $attributes);
        $key = isset($attributes[id])? $name.'_'.$attributes[id]: $name.'_'.$this->getNodesSize();
        $this->setNode($key, $tmpObject->getSeq());
     return $tmpObject;
    }
    protected function removeNodeByName($rootNodeObj, $name)
    {
        $rootNodeObj->removeNodeById($this->getNodeId($name));
        if(sizeof($this->nodes)==1)
            $this->nodes = array();
     else
      unset($this->nodes[$name]);
    }
    protected function getNodeByName($rootNodeObj, $name=null)
    {
     if($name==null)
     {
            $tmpList = array();
            $tmpIds = $this->getNodeId();
            foreach($tmpIds as $key=>$id)
             $tmpList[$key] = $rootNodeObj->getNodeById($id);
            return $tmpList;
     }
     else
     {
      $id = $this->getNodeId($name);
      if($id===null)
      {
             $tmpIds = $this->getNodeId();
             foreach($tmpIds as $tkey=>$tid)
             {
              if(strpos($key, $name)==0)
              {
               $id = $tid;
               break;
              }
             }
      }
      return $rootNodeObj->getNodeById($id);
     }
    }
    public function findNodeByPath($path)
    {
     $pos = strpos($path, '|');
     if($pos<=0)
     {
      return $this->getNode($path);
        }
        else
        {
         $tmpObj = $this->getNode(substr($path, 0, $pos));
         return is_object($tmpObj)? $tmpObj->findNodeByPath(substr($path, $pos+1)): null;
        }
    }
    public function getSaveData()
    {
        $data = $this->values;
        if(sizeof($this->attributes)>0)
         $data[attrs] = $this->attributes;
        $nodeList = $this->getNode();
        if($nodeList==null)
         return $data;
        foreach($nodeList as $key=>$node)
        {
         $data[$key] = $node->getSaveData();
        }
     return $data;
    }
    public function getSaveXml($level=0)
    {
     $prefixSpace = str_pad("", $level, "\t");
     $str = "$prefixSpace<$this->nodeTag";
        foreach($this->attributes as $key=>$value)
         $str .= " $key=\"$value\"";
        $str .= ">\r\n";
        foreach($this->values as $key=>$value){
            if(is_array($value))
            {
                $str .= "$prefixSpace\t<$key";
                foreach($value[attrs] as $attkey=>$attvalue)
                 $str .= " $attkey=\"$attvalue\"";
                $tmpStr = $value[value];
            }
            else
            {
             $str .= "$prefixSpace\t<$key";
             $tmpStr = $value;
            }
            $tmpStr = trim(trim($tmpStr, "\r\n"));
            $str .= ($tmpStr===null || $tmpStr==="")? " />\r\n": ">$tmpStr</$key>\r\n";
        }
        foreach($this->getNode() as $node)
         $str .= $node->getSaveXml($level+1)."\r\n";
     $str .= "$prefixSpace</$this->nodeTag>";
     return $str;
    }
    function __destruct()
    {
     unset($this->nodes, $this->attributes, $this->values);
    }
}
?>

产业新闻

使用PHP 5.0 轻松解析XML文档

Tags: ,

用sax方式的时候,要自己构建3个函数,而且要直接用这三的函数来返回数据, 要求较强的逻辑。 在处理不同结构的xml的时候, 还要重新进行构造这三个函数,麻烦!

用dom方式,倒是好些,但是他把每个节点都看作是一个node,操作起来要写好多的代码, 麻烦!

网上有好多的开源的xml解析的类库, 以前看过几个,但是心里总是觉得不踏实,感觉总是跟在别人的屁股后面.

这几天在搞java, 挺累的,所以决定换换脑袋,写点php代码,为了防止以后xml解析过程再令我犯难,就花了一天的时间写了下面一个xml解析的类,于是就有了下面的东西。

实现方式是通过包装”sax方式的解析结果”来实现的. 总的来说,对于我个人来说挺实用的,性能也还可以,基本上可以完成大多数的处理要求。

功能:

1、 对基本的xml文件的节点进行 查询 / 添加 / 修改 / 删除 工作.

2、导出xml文件的所有数据到一个数组里面.

3、整个设计采用了oo方式,在操作结果集的时候, 使用方法类似于dom

缺点:

1、 每个节点最好都带有一个id(看后面的例子), 每个“节点名字”=“节点的标签_节点的id”,如果这个id值没有设置,程序将自动给他产生一个id,这个id就是这个节点在他的上级节点中的位置编号,从0开始。

2、 查询某个节点的时候可以通过用“|”符号连接“节点名字”来进行。这些“节点名字”都是按顺序写好的上级节点的名字。

使用说明:

运行下面的例子,在执行结果页面上可以看到函数的使用说明

代码是通过php5来实现的,在php4中无法正常运行。

由于刚刚写完,所以没有整理文档,下面的例子演示的只是一部分的功能,代码不是很难,要是想知道更多的功能,可以研究研究源代码。

目录结构:

test.php
      test.xml
      xml / SimpleDocumentBase.php
      xml / SimpleDocumentNode.php
      xml / SimpleDocumentRoot.php
      xml / SimpleDocumentParser.php

文件:test.xml

<?xml version="1.0" encoding="GB2312"?>
<shop>
<name>华联</name>
<address>北京长安街-9999号</address>
<desc>连锁超市</desc>
<cat id="food">
<goods id="food11">
<name>food11</name>
<price>12.90</price>
</goods>
<goods id="food12">
<name>food12</name>
<price>22.10</price>
<desc creator="hahawen">好东西推荐</desc>
</goods>
</cat>
<cat>
<goods id="tel21">
<name>tel21</name>
<price>1290</price>
</goods>
</cat>
<cat id="coat">
<goods id="coat31">
<name>coat31</name>
<price>112</price>
</goods>
<goods id="coat32">
<name>coat32</name>
<price>45</price>
</goods>
</cat>
<special id="hot">
<goods>
<name>hot41</name>
<price>99</price>
</goods>
</special>
</shop>

文件:test.php

<?php
    require_once "xml/SimpleDocumentParser.php";
require_once "xml/SimpleDocumentBase.php";
require_once "xml/SimpleDocumentRoot.php";
require_once "xml/SimpleDocumentNode.php";
$test = new SimpleDocumentParser();
$test->parse("test.xml");
$dom = $test->getSimpleDocument();
echo "<pre>";
echo "<hr><font color=red>";
echo "下面是通过函数getSaveData()返回的整个xml数据的数组";
echo "</font><hr>";
print_r($dom->getSaveData());
echo "<hr><font color=red>";
echo "下面是通过setValue()函数,给给根节点添加信息,添加后显示出结果xml文件的内容";
echo "</font><hr>";
$dom->setValue("telphone", "123456789");
echo htmlspecialchars($dom->getSaveXml());
echo "<hr><font color=red>";
echo "下面是通过getNode()函数,返回某一个分类下的所有商品的信息";
echo "</font><hr>";
$obj = $dom->getNode("cat_food");
$nodeList = $obj->getNode();
foreach($nodeList as $node){
$data = $node->getValue();
echo "<font color=red>商品名:".$data[name]."</font><br>";
print_R($data);
print_R($node->getAttribute());
}
echo "<hr><font color=red>";
echo "下面是通过findNodeByPath()函数,返回某一商品的信息";
echo "</font><hr>";
$obj = $dom->findNodeByPath("cat_food|goods_food11");
if(!is_object($obj)){
echo "该商品不存在";
}else{
$data = $obj->getValue();
echo "<font color=red>商品名:".$data[name]."</font><br>";
print_R($data);
print_R($obj->getAttribute());
}
echo "<hr><font color=red>";
echo "下面是通过setValue()函数,给商品\"food11\"添加属性, 然后显示添加后的结果";
echo "</font><hr>";
$obj = $dom->findNodeByPath("cat_food|goods_food11");
$obj->setValue("leaveword", array("value"=>"这个商品不错",
"attrs"=>array("author"=>"hahawen", "date"=>date('Y-m-d'))));
echo htmlspecialchars($dom->getSaveXml());
echo "<hr><font color=red>";
echo "下面是通过removeValue()/removeAttribute()函数,
给商品\"food11\"改变和删除属性, 然后显示操作后的结果";
echo "</font><hr>";
$obj = $dom->findNodeByPath("cat_food|goods_food12");
$obj->setValue("name", "new food12");
$obj->removeValue("desc");
echo htmlspecialchars($dom->getSaveXml());
echo "<hr><font color=red>";
echo "下面是通过createNode()函数,添加商品, 然后显示添加后的结果";
echo "</font><hr>";
$obj = $dom->findNodeByPath("cat_food");
$newObj = $obj->createNode("goods", array("id"=>"food13"));
$newObj->setValue("name", "food13");
$newObj->setValue("price", 100);
echo htmlspecialchars($dom->getSaveXml());
echo "<hr><font color=red>";
echo "下面是通过removeNode()函数,删除商品, 然后显示删除后的结果";
echo "</font><hr>";
$obj = $dom->findNodeByPath("cat_food");
$obj->removeNode("goods_food12");
echo htmlspecialchars($dom->getSaveXml());
?>

php

2009/11/12

Php与java结合解析xml

Tags: , ,

首先如果你对JAVA与PHP不感兴趣或者说也不打算学习两者的话请不要浪费时间在这里。

请先安装JAVA的执行环境与PHP结合,具体参考http://www.phpx.com/happy/thr78795.html或者下载http://www.javax.org/old/download/JavaXml.rar
里面我写有个readme.txt说明文档,这个压缩包是个简单的例子。有其他问题请联系我。

一开始我想用PHP解析XML,但发现PHP要解析XML有点麻烦,好象PHP5比较方便了,但我这边没有装5,还是4.3。后来我看到IBM一篇文章讲到可以利用JAVA来做,所以我现在就试验了一下。

要解析的XML文件:first.xml,内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<product>
<hello>
<name>小曾</name>
<age>20</age>
</hello>
<hello>
<name>雨伞</name>
<age>20</age>
</hello>
</product>
其中<product>是根,<hello>是节点,一共有两个,name值名字不一样,一个是小曾一个是雨伞,年龄一样。

下面看PHP解析时候的文件:

<?php
/*
作者:雨伞
时间:2004/12/28
*/

$JavaXml = new Java(”JavaXml”); //这里是生成一个我写的JAVA解析XML数据的类
$JavaXml->init(); //这里为初始化,比如取global.properties文件里的XML文件目录(当然你下载了例子以后要改成你的XML文件目录)
$JavaXml->Parse(”first.xml”); //指定要解析的文件,相对于global.properties文件里指定的目录下
$JavaXml->get(0); //这里为取得第一个节点
echo $JavaXml->getValue(”name”).”<br>”; //取得第一个节点name标签值
echo $JavaXml->getValue(”age”).”<br>”; //取得第一个节点age标签值
$JavaXml->setValue(”name”,”大头爸爸”); //设置第一个节点name标签值为大头爸爸
$JavaXml->get(1); //这里为取得第二个节点
echo $JavaXml->getValue(”name”).”<br>”; //取得第二个节点name标签值
echo $JavaXml->getValue(”age”).”<br>”; //取得第二个节点age标签值
?>

$JavaXml->get(0);取得节点位置,比如我的XML文件有两组<hello>,这里get(0)的话就是取第一组的<hello> ,get(1)就是取第二组的<hello>.

最后输出是
小曾
20
雨伞
20

因为$JavaXml->setValue(”name”,”大头爸爸”); 这句修改了第一个节点的name标签的值,XML文件已经被更新过了,所以当再执行一次这个PHP文件的时候结果会成为
大头爸爸
20
雨伞
20

以上简单几句就解析完了,下面是我的JAVA类,里面用到了JDOM来解析XML。

import org.jdom.* ;
import org.jdom.output.* ;
import org.jdom.input.* ;
import java.io.* ;
import java.util.*;
/*
作者:雨伞
时间:2004/12/28
*/
public class JavaXml {
public String path=null;
public String XmlFileName=null;
public SAXBuilder sax=null;
public Document doc=null;
public Element root=null;
public List xlist=null;
public Element e=null;
public Element value=null;

public String getTest(){
return new String(”haha”);
}
public JavaXml(){

}

public String init(){

InputStream is = getClass().getResourceAsStream(”global.properties”);
Properties dbProps = new Properties();
try {
dbProps.load( is ) ;
}
catch ( Exception e ) {
return (”error file”);
}
this.path=dbProps.getProperty(”XmlPath”);
return (”ok”);
}

public void get(int child){
this.e=(Element)xlist.get(child);
}
public String getValue(String name){
this.value=e.getChild(name);
return this.value.getText();
}
public void setValue(String name,String value)throws Exception{
this.value=e.getChild(name);
this.value.setText(value);
XMLOutputter xmlout=new XMLOutputter();
xmlout.output(doc,new FileOutputStream(path+XmlFileName));
}

public void Parse(String XmlFileName)
throws Exception
{
this.XmlFileName=XmlFileName;
this.sax=new SAXBuilder();
this.doc=sax.build(new FileInputStream(path+XmlFileName));
this.root=doc.getRootElement();
this.xlist=root.getChildren();
}

}

PHP也有解析XML,但太麻烦了,刚刚我看了一下PHP5提供的解析XML的方法,它是提供了几个PHP文件来解析XML,PHP4解析XML切没有多少资料可找,也不是很方便,所以这个可以针对PHP4来应用。

php

2009/11/11

在PHP中开发XML应用程序

Tags: ,

一、 XML简介

XML(可扩展的标注语言)是一种W3C标准,主要用于Web应用程序和服务器之间实现容易的交互、数据的存储与使用。

使用XML标准编码的数据具有能容易被人和计算机解释的意义和结构。XML数据是平台和应用程序独立的。不用多说,这本身就使XML成为适合于互联网的 一个理想的数据交换格式(事实上,它正是因这一用途而被开发的)。最近,宽带连接的增长及消费者对于越过任何媒体进行数据共享的应用软件的需求意味 着,XML Web服务和应用软件正变得越来越丰富。

XML的发明正是为了解决描述网上丰富的数据的组织问题;而目前为止,这一问题仅能够通过HTML的巧妙使用得到部分地解决。

下面是一XML文档的实例:

<?xml version=”1.0″?>
<party>
<location>My House</location>
<time>7pm</time>
<guest>
<name>John Bloggs</name>
<item>Crate of Fosters</item>
</guest>
<guest>
<name>Sara Bloggs</name>
<item>Umbrella</item>
</guest>
<guest>
<name>David Fig</name>
<item>Bombay Mix</item>
</guest>
</party>

如果你以前没见过XML,那么你可以认为它看起来象HTML。HTML是一种SGML应用程序,而XML是它的一个子集。然而,其相似性还包括它们具有相似的标注分隔符。

仅需看一下上面的XML片断,我们就能看到,该数据是描述一个具有一些客人的聚会;其中,每一个客人相应于一项。用于描述数据的标签名完全由作者来选 择。所有XML标准要求:数据必须是一致的并且用于描述数据的标签为良构的。我们可以进一步用一种文档类型声明(DTD)或一个XML模式来强制数据的完 整性。然而为简化起见,我们在本文中将仅使用普通的XML。

二、 XML应用程序

刚才,我们已经看到了如何使用XML来描述任何种类的数据。事实上,XML已经在今天的许多Web应用程序中得到广泛使用,下面是一些著名的应用描述:

· XHTML-这是使用最广泛的XML应用程序之一。它类似基于HTML的SGML-用于描述数据在网页上的显示方式。XHTML使用一DTD来确保所有的 文档遵循标准。XHTML的出现使Web程序员的开发稍微容易了一些;然而,一种完全兼容于CSS和XHTML标准的web浏览器尚未出现。

· XML-RPC-远程过程调用(RPC),应用于分布式应用程序中以调用远程计算机上的过程。XML-RPC使用XML对关于过程调用的信息进行编码,并 且使用HTTP把它发送到接收计算机。然后,过程的返回值被再次用XML编码并用HTTP连接发送回调用者计算机。

· RSS-真正简单的聚合/丰富的站点摘要,它是一种用来聚合web站点内容(例如新闻、文章、共享价格和链接等)的方法,它用一个特殊的应用程序(一个聚合器)定期更新用户PC上的RSS回馈。该RSS数据是使用XML进行编码和传输的。

· AJAX-异步的JavaScript和XML,允许web开发者创建具有丰富特征的事件驱动的运行在web浏览器上的web应用程序。其 中,JavaScript用于把XML编码的数据发送到服务器端脚本(或从服务器端接收XML编码的数据),并允许局部的实时的页面更新而不需要更新所有 页面内容。

上面仅仅是XML的可能的应用的一部分。在以后文章中,我们将分析如何在PHP中使用这些应用软件。

三、 在PHP中使用XML

自从PHP 5.0以来,PHP能与XML交互的可用选项显著地增加。而PHP版本4所能提供的是不稳定的而且是非w3c兼容的DOM XML扩展。
下面,我将集中讨论PHP 5所提供给我们的三个允许我们与XML交互的方法:DOM,简单XML和XPath。在可能之处,我将建议最适合于每种方法的条件和数据。所有的示例代码将使用XML数据源来描述一个库及其中包含的书。

<xml version=”1.0″?>
<library>
<categories>
<category cid=”1″>Web Development</category>
<category cid=”2″>Database Programming</category>
<category cid=”3″>PHP</category>
<category cid=”4″>Java</category>
</categories>
<books>
<book>
<title>Apache 2</title>
<author>Peter Wainwright</author>
<publisher>Wrox</publisher>
<category>1</category>
</book>
<book>
<title>Advanced PHP Programming</title>
<author>George Schlossnagle</author>
<publisher>Developer Library</publisher>
<category>1</category>
<category>3</category>
</book>
<book>
<title>Visual FoxPro 6 – Programmers Guide</title>
<author>Eric Stroo</author>
<publisher>Microsoft Press</publisher>
<category>2</category>
</book>
<book>
<title>Mastering Java 2</title>
<author>John Zukowski</author>
<publisher>Sybex</publisher>
<category>4</category>
</book>
</books>
</library>

四、 DOM

DOM PHP扩展名允许使用W3C DOM API在XML文档上进行操作。在PHP 5出现之前,这是PHP能存取XML文档的唯一方法。如果你在JavaScript中使用了DOM,那么会认识到这些对象模型几乎是一样的。

由于DOM方法在遍历和操作XML文档时比较罗嗦,所以任何DOM兼容的代码都有明显的优点-与任何其它实现相同的W3C兼容的对象模型的API兼容。

在下面的实例代码中,我们使用DOM来显示关于每本书的信息。首先,我们遍历一下列表目录,把它们的Id和相应的名字装载到一个索引数组中。然后,我们显示每本书的一个简短描述:

PHP:

<?php
/*这里我们必须指定XML版本:也即是1.0 */
$xml = new DomDocument(’1.0′);
$xml->load(’xml/library.xml’);
/*首先,创建一个目录列表*/
$categories = array();
$XMLCategories = $xml->getElementsByTagName(’categories’)->item(0);
foreach($XMLCategories->getElementsByTagName(’category’) as $categoryNode) {
/*注意我们是如何得到属性的*/
$cid = $categoryNode->getAttribute(’cid’);
$categories[$cid] = $categoryNode->firstChild->nodeValue;
}
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?
php foreach($xml->getElementsBytagName(’book’) as $book):
/*查找标题*/
$title = $book->getElementsByTagName(’title’)->item(0)->firstChild->nodeValue;
/*查找作者-为了简化起见,我们假设仅仅有一个作者*/
$author = $book->getElementsByTagName(’author’)->item(0)->firstChild->nodeValue;
/* 列表目录*/
$bookCategories = $book->getElementsByTagName(’category’);
$catList = ”;
foreach($bookCategories as $category) {
$catList .= $categories[$category->firstChild->nodeValue] . ‘, ‘;
}
$catList = substr($catList, 0, -2); ?>
<div>
<h2><?php echo($title) ?></h2>
<p><b>Author:</b>: <?php echo($author) ?></p>
<p><b>Categories: </b>: <?php echo($catList) ?></p>
</div>
<? php endforeach; ?>
</html>

再提一下,修改XML是较麻烦的。例如,添加一个目录的代码如下:

PHP:

function addCategory(DOMDocument $xml, $catID, $catName) {
$catName = $xml->createTextNode($catName); //创建一个结点以存储文本
$category = $xml->createElement(’category’); //创建一个目录元素
$category->appendChild($catName); //把文本添加到目录元素上
$category->setAttribute(’cid’, $catID); //设置目录的ID
$XMLCategories = $xml->getElementsByTagName(’categories’)->item(0);
$XMLCategories->appendChild($category); //添加新目录
}

五、 保存XML

你可以使用save()和saveXML()方法之一来把DOM描述转换回XML字符串描述。save()方法用一指定的命名把XML保存到一个文件中,而saveXML()从文档的部分或整体中返回一个字符串。

$xml->save(’xml/library.xml’);
//保存全部文件
$categories=$xml->saveXML($XMLCategories);
//返回一个包含种类的字符串
为了说明把DOM兼容的代码移植到另外的语言是如何容易,下面是用JavaScript形式实现的与以上功能相同的代码:

为了说明把DOM兼容的代码移植到另外的语言是如何容易,下面是用JavaScript形式实现的与以上功能相同的代码:

Javascript:
function doXML(){
/* 首先创建一个种类列表*/
var categories = Array();
var XMLCategories = xml.getElementsByTagName(’categories’)[0];
var theCategories = XMLCategories.getElementsByTagName(’category’);
for (var i = 0; i < theCategories.length; i++) {
/* 注意我们是怎样得到属性的*/
var cid = theCategories[i].getAttribute(’cid’);
categories[cid] = theCategories[i].firstChild.nodeValue;
}
var theBooks = xml.getElementsByTagName(’book’);
for(var i = 0; i < theBooks.length; i++) {
var book = theBooks[i];
/* 查找标题*/
var title = book.getElementsByTagName(’title’)[0].firstChild.nodeValue;
/* 查找作者-为简单起见,我们假定仅有一个作者*/
var author = book.getElementsByTagName(’author’)[0].firstChild.nodeValue;
/* 列出种类*/
var bookCategories = book.getElementsByTagName(’category’);
var catList = ”;
for(var j = 0; j < bookCategories.length; j++) {
catList += categories[bookCategories[j].firstChild.nodeValue] + ‘, ‘;
}
catList = catList.substring(0, catList.length -2);
document.open();
document.write(”<h2>” + title + “</h2>”);
document.write(”<p><b>Author:</b>: ” + author + “</p>”);
document.write(”<p><b>Categories: </b>: ” + catList + “</p>”);
}
document.close();
}

六、 简单XML

简单XML确实简单。它允许使用对象和数组存取方法来存取一个XML文档及其元素和属性。操作方式很简单:

· 元素(Element)-这些被描述为SimpleXMLElement对象的单个属性。当有多个作为文档或元素的子元素存在时,每个元素能被使用数组索引标志加以存取。

$xml->books;//返回元素”books”
$xml->books->book[0];//返回在books元素中的第一本书
· 属性(Attribute)-元素的属性是通过关联数组标志来存取和设置的,此时每一个索引对应于一个属性名。

$category['cid'];//返回cid属性的值
· 元素数据(Element Data)-为了检索包含在一个元素内的文本数据,必须使用(string)显式地把它被转换为一个字符串或使用print或echo输出它。如果一个元素包含多个文本结点,那么它们将按被找到的顺序连接起来。

echo ($xml->books->book[0]->title);//显示第一本书的标题
下面是使用简单XML进行转换的原来的实例。为了装载XML文件,我们使用simplexml_load_file()函数,由它来分析该XML文件并且把它装载进一个SimpleXMLElement对象中:

PHP:

<?php
$xml = simplexml_load_file(’xml/library.xml’);
/* 把一个列表的目录装载到一个数组中*/
$categories = array();
foreach($xml->categories->category as $category) {
$categories[(string) $category['cid']] = (string) $category;
}
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?php foreach($xml->books->book as $book):
/* 列举目录*/
$catList = ”;
foreach($book->category as $category) {
$catList .= $categories[((string) $category)] . ‘, ‘;
}
$catList = substr($catList, 0, -2); ?>
<div>
<h2><?php echo($book->title) ?></h2>
<p><b>Author:</b>: <?php echo($book->author) ?></p>
<p><b>Categories: </b>: <? php echo($catList) ?></p>
</div>
<? php endforeach; ?>
</html>

七、 修改XML

尽管文本数据和属性值可以通过使用简单XML加以设置,但是不能新建这些对象。然而,SimpleXM的确提供了一种方法来实现DomElement对 象和DomElement对象之间的转换。为此,我修改了addCategory()函数来说明如何使用simplexml_import_dom()函 数以添加目录和把该文档转换回简单的XML格式:

PHP:

function addCategory(SimpleXMLElement &$sXML, $catID, $catName) {
$xml = new DOMDocument;
$xml->loadXML($sXML->asXML());
$catName = $xml->createTextNode($catName); //创建一个结点来存放该文本
$category = $xml->createElement(’category’); //创建一个目录元素
$category->appendChild($catName); //把文本添加到目录元素
$category->setAttribute(’cid’, $catID); //设置目录id
$XMLCategories = $xml->getElementsByTagName(’categories’)->item(0);
$XMLCategories->appendChild($category); //添加新目录
$sXML = simplexml_import_dom($xml);
return $sXML;
}
同样,SimpleXMLElement对象的asXML()函数可以用来检索XML字符串并把它保存回一个文件中。

八、 xPath

毫无疑问,Xpath是”XML蛋糕之上的樱桃”。XPath允许你使用象SQL一样的查询来查找一个XML文档中的特定信息。DOM和SimpleXML都有内置的对XPath的支持,如SQL,可以被用来提取你想从一XML文档中提取的任何内容。

· //category-查找所有的在文档中出现的任何category。

· /library/books-查找所有作为library的孩子出现的books

· /library/categories/category[@cid]-查找所有作为library/categories的孩子出现且属性为cid的category。

· /library/categories/category[@att='2']-查找所有作为library/categories的孩子且具有属性cid的值为2出现的category。

· /library/books/book[title='Apache 2']-查找所有作为/library/books的孩子且其标题元素有一个值为Apache 2出现的book。

其实,这仅是xPath冰山之一角。你可以使用xPath来创建大量复杂的查询以便从你的文档中提取几乎任何信息。我再次修改了示例代码来向你展示使用xPath是多么轻松愉快的事情。

PHP:

<?php
$xml = simplexml_load_file(’xml/library.xml’);
?>
<html>
<head>
<title>XML Library</title>
</head>
<body>
<?php foreach(((array)$xml->xpath(”/library/books/book”)) as $book):
/*列表目录*/
$catList = ”;
foreach($book->category as $category) {
/*得到具有这个ID的目录*/
$category = $xml->xpath(”/library/categories/category[@cid='$category']“);
$catList .= (string) $category[0] . ‘, ‘;
}
$catList = substr($catList, 0, -2); ?>
<div>
<h2><?php echo($book->title) ?></h2>
<p><b>Author:</b>: <?php echo($book->author) ?></p>
<p><b>Categories: </b>: <?php echo($catList) ?></p>
</div>
<?php endforeach; ?>
</html>

九、 DOM和XPath

在DOM中计算XPath查询需要创建一个DOMXPath对象,下面的evaluate()函数返回一个DOMElement数组。

$xPath = new DOMXPath($xml);
$xPath->evaluate(”/library/books/book[title='Apache 2']“);
十、 结论

现在,我们学习了如何使用了PHP提供给我们的工具来与XML交互。至此,我们已经被”武装起来”并准备好深入钻研XML应用程序了。在下一篇文章中,我们将讨论AJAX及其如何应用于象Google这样的站点开发的。

网站建设