DOM Error - ID 'someAnchor' already defined in Entity, line:

quamagocugh

New Member
If I try to load an HTML document into PHP DOM i get an error along the lines of :\[code\]Error DOMDocument::loadHTML() [domdocument.loadhtml]: ID someAnchor already defined in Entity, line: 9\[/code\]I cannot work out why.Here is some code that loads an html string into DOM. First without containing an anchor tag and second with one. The second document produces an error.Hopefully you should be able to cut and paste it into a script and run it to see the same output:\[code\]<?phpini_set('display_errors', 1);error_reporting(E_ALL);$stringWithNoAnchor = <<<EOT<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body ><h1>Hello</h1></body></html>EOT;$stringWithAnchor = <<<EOT<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body ><h1>Hello</h1><a name="someAnchor" id="someAnchor"></a></body></html>EOT;class domGrabber { public $_FileErrorStr = ''; /** *@desc DOM object factory does the work of loading the DOM object */ public function getLoadAsDOMObj($htmlString) { $this->_FileErrorStr =''; //reset error container $xmlDoc = new DOMDocument(); set_error_handler(array($this, '_FileErrorHandler')); // Warnings and errors are suppressed $xmlDoc->loadHTML($htmlString); restore_error_handler(); return $xmlDoc; } /** *@desc public so that it can catch errors from outside this class */ public function _FileErrorHandler($errno, $errstr, $errfile, $errline) { if ($this->_FileErrorStr === null) { $this->_FileErrorStr = $errstr; } else { $this->_FileErrorStr .= (PHP_EOL . $errstr); } } }$domGrabber = new domGrabber();$xmlDoc = $domGrabber->getLoadAsDOMObj($stringWithNoAnchor );echo 'PHP Version: '. phpversion() .'<br />'."\n";echo '<pre>';print $xmlDoc->saveXML();echo '</pre>'."\n";if ($domGrabber->_FileErrorStr) { echo 'Error'. $domGrabber->_FileErrorStr; }$xmlDoc = $domGrabber->getLoadAsDOMObj($stringWithAnchor);echo '<pre>';print $xmlDoc->saveXML();echo '</pre>'."\n";if ($domGrabber->_FileErrorStr) { echo 'Error'. $domGrabber->_FileErrorStr; }\[/code\]I get the following out put in my Firefox source code view:\[code\]PHP Version: 5.2.9<br /><pre><?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body><h1>Hello</h1></body></html></pre><pre><?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body><h1>Hello</h1><a name="someAnchor" id="someAnchor"></a></body></html></pre>ErrorDOMDocument::loadHTML() [<a href='http://stackoverflow.com/questions/3614586/domdocument.loadhtml'>domdocument.loadhtml</a>]: ID someAnchor already defined in Entity, line: 9\[/code\]So, why is DOM saying that someAnchor is already defined?Thanks in advance on any suggestions.update:Artefacto I think you are right.I experimented with both
  • Instead of using loadHTML() I used the loadXML() method - and that fixed it
  • Instead of having both id and name I used just id - Attribute and that fixed it.
See the comparison script here for the sake of completion:\[code\]<?phpini_set('display_errors', 1);error_reporting(E_ALL);$stringWithNoAnchor = <<<EOT<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body ><p>stringWithNoAnchor</p></body></html>EOT;$stringWithAnchor = <<<EOT<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body ><p>stringWithAnchor</p><a name="someAnchor" id="someAnchor" ></a></body></html>EOT;$stringWithAnchorButOnlyIdAtt = <<<EOT<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>My document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head><body ><p>stringWithAnchorButOnlyIdAtt</p><a id="someAnchor"></a></body></html>EOT;class domGrabber { public $_FileErrorStr = ''; public $useHTMLMethod = TRUE; /** *@desc DOM object factory does the work of loading the DOM object */ public function loadDOMObjAndWriteOut($htmlString) { $this->_FileErrorStr =''; $xmlDoc = new DOMDocument(); set_error_handler(array($this, '_FileErrorHandler')); // Warnings and errors are suppressed if ($this->useHTMLMethod) { $xmlDoc->loadHTML($htmlString); } else { $xmlDoc->loadXML($htmlString); } restore_error_handler(); echo "<h1>"; echo ($this->useHTMLMethod) ? 'using xmlDoc->loadHTML() ' : 'using $xmlDoc->loadXML()'; echo "</h1>"; echo '<pre>'; print $xmlDoc->saveXML(); echo '</pre>'."\n"; if ($this->_FileErrorStr) { echo 'Error'. $this->_FileErrorStr; } } /** *@desc public so that it can catch errors from outside this class */ public function _FileErrorHandler($errno, $errstr, $errfile, $errline) { if ($this->_FileErrorStr === null) { $this->_FileErrorStr = $errstr; } else { $this->_FileErrorStr .= (PHP_EOL . $errstr); } } }$domGrabber = new domGrabber();echo 'PHP Version: '. phpversion() .'<br />'."\n";$domGrabber->useHTMLMethod = TRUE; //DOM->loadHTML$domGrabber->loadDOMObjAndWriteOut($stringWithNoAnchor);$domGrabber->loadDOMObjAndWriteOut($stringWithAnchor );$domGrabber->loadDOMObjAndWriteOut($stringWithAnchorButOnlyIdAtt);$domGrabber->useHTMLMethod = FALSE; //use DOM->loadXML$domGrabber->loadDOMObjAndWriteOut($stringWithNoAnchor);$domGrabber->loadDOMObjAndWriteOut($stringWithAnchor );$domGrabber->loadDOMObjAndWriteOut($stringWithAnchorButOnlyIdAtt);\[/code\]
 
Back
Top