I've extracted the categories you see on the left of this page by creating and exploring the DOM tree of the page. Now I want to create a new DOM to store it on my server and reload it locally and speed-up the whole process. I decided to do that while exploring the original DOM. The exploration of the original DOM works, so assume that the parameters are correct.
I write this code to create the DOM:
$curr_lev=1;
$mydom=new DOMdocument();
$curr_parent=$mydom->createElement('products');
function create_dom($name, $link, $lev){
global $curr_lev;
global $curr_parent;
global $mydom;
switch ($lev){
case $curr_lev:
$curr_parent->appendChild($mydom->createElement($name, $link));
break;
case $curr_lev-1:
$curr_parent=$curr_padre->parentNode;
$curr_parent->appendchild($mydom->createElemnt($name, $link));
break;
case $curr_lev+1:
$curr_parent=$curr_padre->lastChild;
$curr_parent->appendchild($mydom->createElement($name, $link));
break;
}
$curr_lev=$lev;
}
$mydom->formatOutput=TRUE;
$mydom->saveHTMLFile("products.xml");
i try to give an explanation: create_dom()
it's called for each node of the original DOM. $lev
indicates the level of the new node, $curr_lev
it's the level of the last added node, so if they are equal last node added and the current node are child of the same father, if $lev < $curr_lev
we have to go back of one level and the new added node is "brother" of the father of the last added, if $lev > $curr_lev
the current node is child of the last node added.
The first problem is that when I execute I get this error:
Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php:71
Stack trace:
#0 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(71): DOMDocument->createElement('/joomla/compone...', 'Arduino')
#1 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(30): create_dom('Arduino', '/joomla/compone...', 1)
#2 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(38): visita_raff(Object(DOMElement), 1, 'dl')
#3 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php(96): visita_raff(Object(DOMElement), 0, '')
#4 C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\index.php(21): include('C:\Users\Jacopo...') #5 {main} thrown in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php on line 71
$name
usually look like "arduino kit" and $link
is like "/joomla/componenent/virtuamart/..."
I've tried converting it to UTF-8 but it wont work
Also I've tried to do a test and write this code:
function create_xml(){
$mydom=new DOMdocument("1.0", "ISO-8859-1");
$primoElem=$mydom->createElement('foo');
$primoElem->appendChild($mydom->createElement('arduinio', 'http:arduino'));
$mydom->formatOutput=TRUE;
return $mydom->saveXML("foo.xml");
}
I get no error saveXML()
returns 1, but nothing is written to the file!
What am I doing wrong? Please consider that is the first time I work with those things so be gentle :)