RDF Parser (class_rdf_parser.php) |
Description: This class is a port to PHP of the Repat
parser by Jason Diammond. Repat originally written in C was ported to PHP code using the XML parser functions of PHP, the result is a
SAX-like RDF parser in native PHP code. RDF is an initiative of the W3C to describe metadata, you can find everything
about RDF in the W3C site. The class implements an event-driven interface for parsing RDF, when an RDF statement is found a handler defined by you (a PHP function) will be called and you can do whatever you want with the RDF triple (subject,predicate,object). See usage information below and class documentation for a full description of this class. |
NEWS:
|
This class code as well as documentation are hosted at SourceForge please visit our SourceForge page for releases, documentation, bug-tracking, support forums and mailing lists. |
Features | To-dos |
|
|
Contact: Luis Argerich (lrargerich@yahoo.com) |
Detailed description and usage:
The first thing to do is to create an object of the class and then create a new RDF parser: $rdf=new Rdf_parser(); $rdf->rdf_parser_create( NULL ); Now we have to define the handlers to be used, you can use the following methods to set handlers:
The Statement handler The statement handler is the most important handler since it will be the one called when RDF statements are found, we'll define the handler here and then describe each parameter: $rdf->rdf_set_statement_handler("my_statement_handler"); function my_statement_handler(&$user_data, $subject_type, $subject, $predicate, $ordinal,$object_type,$object,$xml_lang ) { // Code here } The statement handler parameters
Other handlers The start and end element handlers are triggered (if set) when some non-RDF XML element is found by the parser, the start_element_handler should receive $user_data,$name and $attributes while the end_element handler receives $user_data and $name. The character data handler This handler is triggered when data is found outside RDF elements (if the handler is set). The handler receives two arguments: $user_data and $data containing the characters found. The warning handler The warning handler is triggered (if set) when an RDF error is detected, teh handler receives one argument: $message containing the description of the error. Parsing To parse a RDF document use the $rdf->rdf_parse($s,$len,$is_final) function you can parse the document by chunks calling this function many times the arguments are: $s is the data to be parsed, $len is the length of the data to be parsed and $is_final is a boolean indicating if this chunk is the last chunk of the document (no more data). The method returns true if everything went well or false if there's an XML error while parsing the document. |
Method Summary | |
Boolean |
rdf_parser_create(string $encoding)
Creates an RDF parser |
void |
rdf_parser_free(string $encoding)
Frees resources allocated by an RDF parser |
void |
rdf_set_user_data(Any $user_data)
Sets a php variable as the parser user data |
void |
rdf_set_statement_handler(string $handler)
Set the handler to be called when statements are found. |
void |
rdf_set_parse_type_literal_handler(string $start, string $end)
Sets handlers for parse type literals |
void |
rdf_set_element_handler(string $start, string $end)
Sets handlers to be called when a non-RDF element starts or ends |
void |
rdf_set_character_data_handler(string $handler)
This allows you to define a handler for text outside RDF elements. |
void |
rdf_set_warning_handler(string $handler)
Sets a warning handler to be called if the RDF document is broken |
Boolean |
rdf_parse(string $s, int $len, boolean $is_final)
Parses a portion of an RDF document or a whole document |
void |
rdf_set_base(string $base)
Sets the base name of the document being parsed |
Method Detail |
Boolean rdf_parser_create(string $encoding)
None
void rdf_parser_free(string $encoding)
None
void rdf_set_user_data(Any $user_data)
None
void rdf_set_statement_handler(string $handler)
None
void rdf_set_parse_type_literal_handler(string $start, string $end)
None
void rdf_set_element_handler(string $start, string $end)
None
void rdf_set_character_data_handler(string $handler)
None
void rdf_set_warning_handler(string $handler)
None
Boolean rdf_parse(string $s, int $len, boolean $is_final)
If an XML error is detected the function returns false.If an RDF error is detected then the function defined by rdf_set_warning_handler is called if set.
void rdf_set_base(string $base)
None