Tuesday, May 22, 2012

GetElementsByTagName alternative to DOMDocument

I am creating an HTML file with DOMDocument, but I have a problem at the time of the search by the getElementsByTagName method. What I found is that as I'm generating the hot, does not recognize the labels that I inserted.



I tried with DOMXPath, but to no avail :S



For now, I've got to do is go through all the children of a node and store in an array, but I need to convert that score DOMNodeList, and in doing



return (DOMNodeList) $ my_array;


generates a syntax error.



My specific question is, how I can do to make a search for tags with the getElementsByTagName method or other alternative I can offer to achieve the task?



Recalling that the DOMDocument I'm generating at the time.



If you need more information, I'll gladly place it in the question.



Sure Jonathan Sampson.



I apologize for the editing of the question the way. I did not quite understand this forum format.



For a better understanding of what I do, I put the inheritance chain.



I have this base class



abstract class ElementoBase {
...
}


And I have this class that inherits from the previous one, with an abstract function insert (insert)



abstract class Elemento extends ElementoBase {
...
public abstract function insertar ( $elemento );
}


Then I have a whole series of classes that represent the HTML tags that inherit from above, ie.



class A extends Elemento {
}
...


Now the code I use to insert the labels in the paper is as follows:



public function insertar ( $elemento ) {

$this->getElemento ()->appendChild ( $elemento->getElemento () );
}


where the function getElemento (), return a DOMElement



Moreover, before inserting the element do some validations that depend on the HTML tag that is to be inserted,
because they all have very specific specifications.



Since I'm generating HTML code at the same time, it is obvious that there is no HTML file.



To your question, the theory tells me to do this:



$myListTags = $this->getElemento ()->getElementsByTagName ( $tag );


but I always returns null, this so I researched it because I'm not loading the HTML file, because if I



$myHtmlFile = $this->getDocumento ()->loadHTMLFile ( $filename );
$myListTags = $myHtmlFile->getElementsByTagName ( $etiqueta );


I do return the list of HTML tags



If you need more information, I'll gladly place it in the question.





1 comment: