0

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 :)

1 Answer 1

1

The Exception DOMException with the message

Invalid Character Error

means that you have tried to create an element (DOMDocument::createElement()) containing invalid characters in the element name:

$mydom->createElement($name, $link)
                        ^
                        |
           first parameter is the element name

In XML not every name is valid, some even contain invalid characters (for example a space " " or the backslash /) or invalid byte-sequences that aren't anything from the Unicode UTF-8 range. DOMDocument in PHP accepts UTF-8 as input only. So for for general. If you want to learn in depth which characters are valid in XML element names you can find more information that you will likely ever need in your live in How to check if string is a valid XML element name?.

So for now if you look closely to the stacktrace of the error message you can probably even spot the problem:

DOMDocument->createElement('/joomla/compone...', 'Arduino') 
                            ^      ^

The / character is not valid inside an XML element name. Fix the issue and you should be able to just add your stuff. Just use an element name that is valid in the end.

Sign up to request clarification or add additional context in comments.

9 Comments

Also, do not use the second argument to createElement as it also makes it easy to produce invalid XML. Use createTextNode() and append that node to the element.
@FrancisAvila: What problem do you mean? Those two should be interchange-able and I didn't spot any difference between the two so far. But you seem to know more, please share.
Try createElement('root','&')--you will get an error and no text will be added to <root/> because the second argument is parsed for entities and entities are not escaped. Thus it's not exactly equivalent to $d->createElement('root')->appendChild($d->createTextNode('&')) and is a fertile source of bugs for people who think it is equivalent. (See also: createElement('r','&amp;') === <r>&amp;</r>, not <r>&amp;amp;</r>!)
@hakre i get your point, but if i try to do that: $curr_parent->appendChild($mydom->createElement('item', $link)); item should be UTF-8, and as far as i now there's no limitation for the charater set for the value of the element right? but i continue to get errors that seems to be generated from other problems: Warning: DOMNode::appendChild(): Couldn't fetch DOMElement in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php on line 79 continue....
Warning: Couldn't fetch DOMElement. Node no longer exists in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php on line 78 Notice: Undefined property: DOMElement::$lastChild in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php on line 78 Fatal error: Call to a member function appendchild() on a non-object in C:\Users\Jacopo\Dropbox\Tirocinio\xampp-portable\htdocs\sites\prova\cerca categorie.php on line 79 what that mean?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.