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 \n");
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()." \n");
}
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 |
get_error()
Returns last error message |
void |
init()
Initializes the parser |
boolean |
parse_file(string $xml)
Parses an XML document from a file or URL |
boolean |
parse(string $data, boolean $is_final)
Parses data |
void |
set_handler(string $path, string $handler_name)
Sets a handler to process XML elements |
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
|