topical media & game development
server-php-xml-class-rdf-parser-class-rdf-parser.htm / htm
<html>
<head>
<title>RDF Parser (class_rdf_parser.php)</title>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
<div align="center">
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#aaaaee" class="text" align="center"><a href="http://phpxmlclasses.sourceforge.net/">PHP XML Classes</a></td>
</tr>
</table>
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" bgcolor="#aaaaee" class="textblbl" align="left">
<a href="http://www.w3.org/RDF/" title="RDF Resource Description
Framework"> <img border="0" src="http://www.w3.org/RDF/icons/rdf_w3c_button.32"
alt="RDF Resource Description Framework Icon"></a>
</td>
<td bgcolor="#aaaaee" class="textblbl" align="center">RDF Parser (class_rdf_parser.php)</td>
</tr>
</table>
<br/>
<table border="0" width="80%">
<tr>
<td bgcolor="#eeeeff" class="text">Description: This class is a port to PHP of the <a href="http://injektilo.org/rdf/repat.html">Repat</a>
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 <a href="http://www.w3.org/RDF/">the W3C site</a>.<br/><br/>
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).<br/>
See usage information below and class documentation for a full description of this class.
</td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
<td valign="top" class="text" width="50%"><b>NEWS:</b>
<ul>
<li> 06-13-2002 First version of this class released.
</ul>
</td>
<td valign="top" width="50%" class="text">
This class code as well as documentation are hosted at <a href="http://www.sourceforge.net">SourceForge</a>
please visit our <a href="http://www.sourceforge.net/projects/phpxmlclasses/">SourceForge page</a> for releases, documentation, bug-tracking, support
forums and mailing lists.
</td>
</tr>
</table>
<br/>
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td class="textbl" bgcolor="aaaaee" width="50%">Resources</td>
<td class="textbl" bgcolor="aaaaee" width="50%">Requirements</td>
</tr>
</table>
<table border="1" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" bgcolor="eeeeff" class="text" width="50%">
<ul>
<li><a class="textbl" href="http://www.sourceforge.net/projects/phpxmlclasses">Downloads</a>
<li><a class="textbl" href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/phpxmlclasses/rdf_parser/">Browse CVS</a>
<li><a class="textbl" href="http://sourceforge.net/tracker/?atid=470965&group_id=53613&func=browse">Report Bugs</a>
<li><a class="textbl" href="http://phpxmlclasses.sourceforge.net/php_rdf_tutorial.pdf">Tutorial (pdf)</a>
</ul>
</td>
<td valign="top" bgcolor="eeeeff" class="text" width="50%">
<ul>
<li>PHP 4.0.5+
</ul>
</td>
</tr>
</table>
<br/>
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td class="textbl" bgcolor="aaaaee" width="50%">Features</td>
<td class="textbl" bgcolor="aaaaee" width="50%">To-dos</td>
</tr>
</table>
<table border="1" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" bgcolor="eeeeff" class="text" width="50%">
<ul>
<li>All the valid RDF XML syntaxes defined by the W3C are supported. (<a href="http://www.w3.org/TR/1999/REC-rdf-syntax-19990222/">See RDF XML syntax spec here</a>)
<li>Can parse RDF embeddded in other XML vocabulaires.
<li>You can define SAX handlers for non-RDF XML elements. Thus allowing you to parse RDF and non-RDF in just one pass.
</ul>
</td>
<td valign="top" bgcolor="ddddff" class="text" width="50%">
<ul>
<li>If you have some to-do in mind let me know it.
</ul>
</td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
<!--
<td valign="top" class="text" width="50%"><b>To-do/future releases:</b>
<ul>
<li>Class
<li>Class
<li>Class
</ul>
</td> -->
<td valign="top" width="100%" class="text">
Contact: Luis Argerich (<a href="mailto:lrargerich@yahoo.com">lrargerich@yahoo.com</a>)
</td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
<!--
<td valign="top" class="text" width="50%"><b>To-do/future releases:</b>
<ul>
<li>Class
<li>Class
<li>Class
</ul>
</td> -->
<td valign="top" width="100%" class="text">
<b>Detailed description and usage:</b>
<br/><br/>
The first thing to do is to create an object of the class and then create a new RDF parser:
<br/><br/>
<pre>
rdf->rdf_parser_create( NULL );
</pre>
<br/>
Now we have to define the handlers to be used, you can use the following methods to set handlers:
<br/><br/>
<ul>
<li>rdf_set_statement_handler(start_handler,warning_handler);
<li>rdf_set_character_data_handler(rdf->rdf_set_statement_handler("my_statement_handler");
function my_statement_handler(&subject_type, predicate,
object_type,xml_lang )
{
// Code here
}
</pre>
<br/><br/>
<b>The statement handler parameters</b>
<br/><br/>
<table border="1">
<tr><td class="text">Parameter</td><td class="text">Description</td></tr>
<tr><td class="text">user_data</td><td class="text">user_data is a php variable that can be set using rdf_set_user_data(user_data,attributes while the end_element handler receives
name.
<b>The character data handler</b>
<br/><br/>
This handler is triggered when data is found outside RDF elements (if the handler is set). The handler receives
two arguments: data containing the characters found.
<b>The warning handler</b>
<br/><br/>
The warning handler is triggered (if set) when an RDF error is detected, teh handler receives one argument: rdf->rdf_parse(len,s is the data to be parsed, 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.
<br/>
</div>
<h3>Documentation</h3>
<h2>Classes</h2>
<ul>
<li><a href="#class28">Rdf_parser</a></li>
</ul>
<a name="class28"></a>
<h3>Rdf_parser</h3>
Extends: None
Description: This class parses RDF documents in any RDF valid syntax according to the RDF specification. Events are produced that can be intercepted by callback PHP functions.
<br>
<!-- METHOD SUMMARY -->
<A NAME="pDomNode_method_summary"><!-- --></A>
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Method Summary</B></FONT></TD>
</TR>
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> Boolean</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class28method1">rdf_parser_create</A></B>(string encoding)</CODE>
<BR>
Frees resources allocated by an RDF parser</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class28method3">rdf_set_user_data</A></B>(Any handler)</CODE>
<BR>
Set the handler to be called when statements are found.</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class28method5">rdf_set_parse_type_literal_handler</A></B>(string end)</CODE>
<BR>
Sets handlers for parse type literals</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class28method6">rdf_set_element_handler</A></B>(string end)</CODE>
<BR>
Sets handlers to be called when a non-RDF element starts or ends</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class28method7">rdf_set_character_data_handler</A></B>(string handler)</CODE>
<BR>
Sets a warning handler to be called if the RDF document is broken</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> Boolean</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class28method9">rdf_parse</A></B>(string len, boolean base)</CODE>
<BR>
Sets the base name of the document being parsed</TD>
</TR>
<!-- A ROW -->
</TABLE>
<P>
<!-- METHOD SUMMARY -->
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=1><FONT SIZE="+2">
<B>Method Detail</B></FONT></TD>
</TR>
</TABLE>
<!-- method -->
<A NAME="class28method1"><!-- --></A>
<H3>rdf_parser_create</H3>
<PRE>
Boolean <B>rdf_parser_create</B>(string encoding - This optional parameter can indicate the encoding that can be used to parse the document, see the XML parser extension of PHP for valid encodings.<br><DT><B>Returns:</B><DD>False if the parser cannot be created<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!-- method -->
<A NAME="class28method2"><!-- --></A>
<H3>rdf_parser_free</H3>
<PRE>
void <B>rdf_parser_free</B>(string user_data)</PRE>
<DL>
<DD>This method allows you to set a PHP variable as "user data", handlers will then receive and can access/modify this variable. Note that this is completely optional.<DD>
<DL>
<DT><B>Parameters:</B><DD>
handler)</PRE>
<DL>
<DD>When the parser sees an RDF statement it will call the method defined with this function.<DD>
<DL>
<DT><B>Parameters:</B><DD>
user_data,subject,ordinal,object,user_data: is a PHP variable passed as user_data (if set)
subject: is the subject of the RDF statement.
ordinal: is the ordinal
object: Is the object of the statement
start, string start - The handler receives just one parameter: &end - The handler receives just one parameter: &start, string start - The handler reeives: &name, att_name=>end - The handler should receive &name containing user_data if set and the name of the ending element.<br><DT><B>Returns:</B><DD>Nothing<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!-- method -->
<A NAME="class28method7"><!-- --></A>
<H3>rdf_set_character_data_handler</H3>
<PRE>
void <B>rdf_set_character_data_handler</B>(string handler - The handler should receive &data containing user_data (if used) and the data found.<br><DT><B>Returns:</B><DD>Nothing<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!-- method -->
<A NAME="class28method8"><!-- --></A>
<H3>rdf_set_warning_handler</H3>
<PRE>
void <B>rdf_set_warning_handler</B>(string handler - The handler receives just one parameter: s, int is_final)</PRE>
<DL>
<DD>This function is used to parse RDF documents, you can process the document by chunks using repetitive calls of this function thus limiting the amount of memory to be used. This allows you to parse huge RDF documents without consuming all the server resources.
Example:
done)
{
input, 512 );
input);
if ( ! buf, strlen(input) ) )
{
// process_error here
}
}<DD>
<DL>
<DT><B>Parameters:</B><DD>
len - This contains the length of the data to be parsed, usually strlen(is_final - For example if you are parsing a file (or URL) use feof(base)</PRE>
<DL>
<DD>If you are parsing a document from a URL use the URL as base, if you are parsing a file use file:://path as the base. This method is important since URIs will be constructed relative to the base when parsing the document.<DD>
<DL>
<DT><B>Parameters:</B><DD>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.