Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1548877
  • 博文数量: 194
  • 博客积分: 6450
  • 博客等级: 准将
  • 技术积分: 2085
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-06 13:39
文章分类

全部博文(194)

文章存档

2013年(38)

2012年(11)

2011年(1)

2010年(1)

2009年(4)

2008年(13)

2007年(18)

2006年(63)

2005年(45)

我的朋友

分类: LINUX

2005-11-11 18:37:55

在XML SAX解析器中 使用XPATH查询XML文档中的指定元素。
实现类似与XML DOM解析器中XPATH相关功能。
来源于sourceforge.net
漫步者 于 2005-11-11

PHP XML Classes
A collection of classes and resources to process XML using PHP

Description: This class implements a generic event-driven parser. User functions can be asociated to specific XML elements indicated by a path, for example /foo/data/name. Whenever the parser detects an element matching a path the user function is called receiving the element name, attributes and its content (xml fragment or text). A function can handle multiple paths or you can use one function for each path. This class can be used to process huge files processing element contents later with Xpath, DOM or XSLT.

Path Parser (class_path_parser.php)

Description: A generic XML parsing class that lets your set-up PHP functions to be called when some XML elements are found.
Using this class you can set handlers for specific XML paths, for example /foo/data/name, for each handler you set up a PHP function in the form foo($name,$attribs,$content) that the parser will call when an element matching the given path is found by the parser.
Summarizing this is a good tool to generalize parsing tasks using SAX/expat from PHP. It can be used as a generic parsing class and is specially useful to retrieve elements from a very large file before processing each element using DOM. (We've parsed 50Mb XML documents with success using this class).

NEWS:
  • 07-04-2002 Documentation updated and package rebuilt.
  • 06-20-2002 First version of this class released.
This class code as well as documentation are hosted at please visit our for releases, documentation, bug-tracking, support forums and mailing lists.

Resources Requirements
  • PHP 4.0.5+

Features To-dos
  • Absolute paths can be parsed.
  • Multiple handlers can be set, a handler can handle multiple paths.
  • Namespaces are supported (local name can be used regardless of the namespace)
  • Support for paths with wildcards is planned example //name or /foo//name

Contact: Luis Argerich ()

Detailed description and usage:

See path_parser.php for an example about how to use the class, this is the code:

include_once("class_path_parser.php");

function name($name,$attribs,$content) {
print("
");
print("Hey $name
");
print_r($attribs);
print("
");
}

$parser = new Path_parser();
$parser->set_handler("/foo/data/name","name");
$parser->set_handler("/foo/data","name");
$parser->set_handler("/foo/data/type/var","name");
if(!$parser->parse_file("foo.xml")) {
print("Error:".$parser->get_error()."
");
}


As you can see using this class is very easy and can provide a lot of very good results. If the document uses namespaces you can set-up paths based on local-names. (A handler setting paths using qualified names is planned for a future release)

Documentation

Classes

Path_parser

Extends: None
Description: Using this class you can parse an XML file setting handlers for specific XML elements defined by paths, for example /foo/data/name or /foo/data, the handlers can receive the element name, attributes and content. This class can be used in several ways incresing XML processing felxibility a lot.

Method Summary
 string ()
          Returns last error message
 void ()
          Initializes the parser
 boolean (string $xml)
          Parses an XML document from a file or URL
 boolean (string $data, boolean $is_final)
          Parses data
 void (string $path, string $handler_name)
          Sets a handler to process XML elements
 

Method Detail

get_error

string get_error()
This function can be used to return last error message when something went wrong.
 
Parameters:

Returns:
The error message
Throws:
None

init

void init()
This method must be called if you plan to parse more than one document using the same object, after parsing a document call init and you are ready to parse a new document.
 
Parameters:

Returns:
Nothing
Throws:
None

parse_file

boolean parse_file(string $xml)
This method can be used to parse an XML document from a file or URL
 
Parameters:
$xml - URI or name of the file containing the document to be parsed
Returns:
True if the document was parsed succesfully, false if there was some error.
Throws:
If an error occurss this method sets an error message that can be recovered with get_error

parse

boolean parse(string $data, boolean $is_final)
This is a generic parsing method it can be used to parse chunks of data.
 
Parameters:
$data - This should contain a chunk of XML data to be parsed
$is_final - This is a boolean var indicating if the chunk passed as the previous argument was the last chunk of data to be parsed by the parser
Returns:
True if the data was parsed succesfully or false if there was some error
Throws:
None

set_handler

void set_handler(string $path, string $handler_name)
This method can be used to process XML elements that match a given pattern, for example /foo or /foo/name or /foo/data/name, etc. You set-up the name of a PHP function to be called when the element is parsed.
 
Parameters:
$path - This is an absolute path from the roor of the XML document for example /foo/data/name will match name elements children of data children of foo (foo is the root element).
$handler_name - The handler must receive the following arguments: $name,$attribs and $content. $name will be the name of the element. $attribs is an array of asocs containing the element attributes and $content will be a string with the element content (text and subelements)
Returns:
Nothing
Throws:
None
阅读(800) | 评论(0) | 转发(0) |
0

上一篇:模板引擎SMARTY

下一篇:XML Spy简易教程

给主人留下些什么吧!~~