I am creating an extended extension and I need to parse the HTML. I need to use DOM inside the extension.
I have copied php_domxml.dll to ext directory of php extensions.
I have write extension=php_domxml.dll in php.ini.
I use the following code but doesn't work. The first line doesn't work because I have checked the value of $doc variable and its null.
$doc = new DOMDocument();
$doc->loadHTML($contingut);
/*** a new dom object ***/
$doc = new domDocument;
/*** discard white space ***/
$doc->preserveWhiteSpace = false;
/*** the table by its tag name ***/
$table = $doc->getElementById('directori');
/*** get all rows from the table ***/
$rows = $table->getElementsByTagName('tr');
/*** loop over the table rows ***/
foreach ($rows as $row)
{
/*** get each column by tag name ***/
$cols = $row->getElementsByTagName('td');
/*** echo the values ***/
$contingut.= $cols->item(0)->nodeValue.'<br />';
$contingut.=$cols->item(1)->nodeValue.'<br />';
$contingut.=$cols->item(2)->nodeValue;
$contingut.='<hr />';
}
Anybody could tell me how to use dom in a typo3 extension?
Thanks in advance
Francesc
You don't need to do anything. A TYPO3 extension itself is also just php Code like any other php script. So you can use any php functionality that already is enabled in php itself on your server. Nearly any php 5.x distribution comes with the library functionalities. If you can execute $doc = new DOMDocument(); without getting a "class not found" error by php, the DOM library is in your system. $doc being NULL must have another reason, it maybe just because on the first line there is of course no DOM structure in $dom just being instanciated from scratch.
the first line is definitely the correct syntax.
It has to be
$doc = new DOMDocument();
not
$doc = new domDocument;
How did you try to check the variable? Using var_dump($doc) should output this:
object(DOMDocument)#1 (0) { }
Thank you for you answers.
I have tried to use this code:
function main($content,$conf) {
$contingut=parent::main($content,$conf);
$doc = new DOMDocument();
if (isset($doc))
{
$contingut.='ole';
}
return $contingut;
}
With this function I get the result of the conent of the page and the text 'ole'. But if i add the following line :
function main($content,$conf) {
$contingut=parent::main($content,$conf);
$doc = new DOMDocument();
if (isset($doc))
{
$contingut.='ole';
}
$doc->loadHTML("<html><body>Test<br></body></html>");
return $contingut;
}
the page appears in blank. I don't get any result.
Any ideas.
Thank you for your ansers and interest.
Francesc
Try writing <br /> instead of <br>. I believe the string must be xhtml-compliant, so empty tags should be self-closing.
check your error_reporting and display_errors setting in your php installation. Maybe you are getting a fatal error and have set display_errors to off. That would be an explenation for a blank page.
Thank you for your answers.
I have discovered that DomDocument loads a simple html code but my need is to load this html in a domDocument and it doesn't load trough $doc->loadHTML($contingut), where $contingut contains the following html code:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!--
This website is powered by TYPO3 - inspiring people to share!
TYPO3 is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.
TYPO3 is copyright 1998-2006 of Kasper Skaarhoj. Extensions are copyright of their respective owners.
Information and contribution at http://typo3.com/ and http://typo3.org/
-->
<link rel="SHORTCUT ICON" href="fileadmin/imatges/favicon.ico" />
<link rel="stylesheet" type="text/css" href="typo3temp/stylesheet_6756561582.css" />
<link rel="stylesheet" type="text/css" href="typo3/sysext/css_styled_content/css/Intranet.css" />
<link rel="icon" href="fileadmin/imatges/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="fileadmin/imatges/favicon.ico" type="image/x-icon"/>
<title>adreces</title>
<meta name="generator" content="TYPO3 4.0 CMS" />
<script type="text/javascript" src="typo3temp/javascript_79af1da1ab.js"></script>
</head>
<body bgcolor="#dddddd" link="#CC6666" vlink="#CC6666" alink="#707070" style="overflow-x:auto;overflow-y:auto">
<table border=0 height=121 class="margesuperior" valign=middle><tr><td valign="top">Intranet de l'Ajuntament</td></tr></table>
<!-- CONTENT ELEMENT, uid:1304/list [begin] -->
<a id="c1304"></a>
<!-- Plugin inserted: [begin] -->
<script src="typo3conf/ext/sg_zfelib/js/user_func.js" type="text/javascript">
</script>
<!--
BEGIN: Content of extension "directoris", plugin "tx_directoris_pi1"
-->
<div class="tx-directoris-pi1">
<br />
<table border=0 cellspacing=0 cellpadding=0>
<tr><td valign="top">
<br />
</td></tr>
<tr><td>
<img src="clear.gif" width="16" height="1" border="0" alt="">
</td></tr>
<tr><td valign="top">
<table id="directori" class="fitxers" border=1px>
<tr>
<th width="350px" valign="top"><b>Nom i Cognoms</b></th>
<th valign="top"><b>Extensió</b></th>
<th valign="top"><b>Telèfon</b></th>
<th valign="top"><b>Mòbil</b></th>
<th valign="top"><b>Correu Electrònic</b></th>
</tr>
<tr>
<td valign="top">Juliana, Francesc </td>
<td valign="top">123</td>
<td valign="top">934578745</td>
<td valign="top">96444564</td>
<td valign="top">xescjp@hotmail.com</td>
</tr>
<tr>
<td valign="top">Juliana PujolarR, Olga </td>
<td valign="top">123</td>
<td valign="top"></td>
<td valign="top">736472364362</td>
<td valign="top">xescjp@hotmail.com</td>
</tr>
<tr>
<td valign="top">uiuy, ui </td>
<td valign="top">uiiu</td>
<td valign="top">ui</td>
<td valign="top">ui</td>
<td valign="top">ui</td>
</tr>
</table>
</td></tr>
</table>
</div>
<!-- END: Content of extension "directoris", plugin "tx_directoris_pi1" -->
<!-- tx_directoris_pi1 duration: 146 ms (firstOfList=20, list=87) -->
<!-- Plugin inserted: [end] -->
<!-- CONTENT ELEMENT, uid:1304/list [end] -->
</body>
</html>
Do you know the reason I can't load this html code in Domdocument?
Thanks in advance.
I appreciate your help very much.
Francesc
You should check if errors occur. DomDocument notifies you if the html you are loading into it, causes problems. By the way, all this is not a TYPO3 issue.
I have disabled php_domxml extension in php.ini and DomDocument have started to work.
It's solved
Thank you for your help.
Francesc