-3

Check for each text node whether or not it exists in DataBase, if it does then add a class to its Element node.

I have tried do it the following way :

function extractText($node) {
    if (XML_TEXT_NODE === $node->nodeType || XML_CDATA_SECTION_NODE === $node->nodeType) {

// am considering that Login is the word that exists in DB
            if ($node->nodeValue == "Login"): 
                $node->setAttribute("class", "translated");
                return $node->nodeName;
            endif;
        } else if (XML_ELEMENT_NODE === $node->nodeType || XML_DOCUMENT_NODE === $node->nodeType
                || XML_DOCUMENT_FRAG_NODE === $node->nodeType) {
            if ('script' === $node->nodeName || 'style' === $node->nodeName)
                return '';

        $text = '';
        foreach ($node->childNodes as $childNode) {
            $text .= extractText($childNode);
        }
        return $text;
    }
}

$doc = new DomDocument;
$doc->loadHTMLFile('test.html');

//var_dump(extractText($doc->getElementsByTagName('body')->item(0)));
echo extractText($doc->getElementsByTagName('body')->item(0));

But it gives Error message.

Fatal error: Call to undefined method DOMText::setAttribute()

atif
  • 1,693
  • 13
  • 38
  • 70
  • It would be too easy if you told us the error message. – MrCode Dec 18 '12 at 10:40
  • @Charles See above error and kindly do not give negative votes straight away – atif Dec 18 '12 at 10:47
  • @MrCode hope now it will be too easy for you to answer and remove the negative vote – atif Dec 18 '12 at 10:48
  • This website is looking for programming questions that are of general interest *and not* for a list of individual requirements that you need to get solved in order. Please follow the [ask advice](http://stackoverflow.com/questions/ask-advice) you needed to confirm before posting *any* question. Keep in mind that only you want something and you ask yourself how it is programmed does not qualify as a programming question per-se. *Edit:* Please also see: [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) – hakre Dec 18 '12 at 10:51

1 Answers1

2

Fatal error: Call to undefined method DOMText::setAttribute()

Well, let's look at the code.

if (XML_TEXT_NODE === $node->nodeType || XML_CDATA_SECTION_NODE === $node->nodeType) {
// am considering that Login is the word that exists in DB
    if ($node->nodeValue == "Login"): 
        $node->setAttribute("class", "translated");
        return $node->nodeName;
    endif;
} /* else ... */

You're asking exclusively for text or CDATA nodes, then you're trying to set an attribute ... on the text. This won't work. Text nodes can't have attributes.

You need to get to the element containing the text and add the attribute there. Because DOMText inherits from DOMNode, you can inspect the parentNode property and probably get an element back.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • ! thanks for the reply, but i tried to do this way too : $node->nodeName->setAttribute("class", "translated"); but of no use – atif Dec 18 '12 at 10:51
  • 1
    Why would you try `nodeName`? `nodeName` is a *string*. It is not a reference an element. Please refer to the linked manual pages. – Charles Dec 18 '12 at 10:52
  • charles can you modify your answer plz so that i can make it to work, currenlty am not able to do that – atif Dec 18 '12 at 11:16
  • No, I will not provide copy/paste code for you. – Charles Dec 20 '12 at 04:52