0

I am trying to learn how to work with xPath and PHP by loading a simple URL as a DOM object and querying the DOM using xPath. I have been running in to a lot of trouble and have been trying to troubleshoot these warnings messages for quite a while now with no success.

I appreciate any suggestions as to why this happening.

Many thanks in advance!

PHP

<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("http://n5a.software.com/calendar/da2013/profile.php?__id=691");

$xpath = new DOMXpath($doc);

$elements = $xpath->query("//ul[@class='flper']/li[2]");

if (!is_null($elements)) {
    foreach ($elements as $element) {
        $nodes = $element->childNodes;
        foreach ($nodes as $node) {
            echo $node->nodeValue. "\n";
        }
    }
}

?>

Output

Warning: DOMDocument::loadHTMLFile(): Attribute style redefined in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 92 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';' in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 101 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Attribute cellspacing redefined in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 109 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : span in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 165 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : script in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 166 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: no name in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 197 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : td in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 205 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : tr in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 230 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : table in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 231 in C:\xampp\htdocs\www\test\xpath.php on line 3
AnchovyLegend
  • 10,873
  • 31
  • 123
  • 211

1 Answers1

3

Disable warnings with libxml_use_internal_errors(true) http://www.php.net/manual/en/function.libxml-use-internal-errors.php

It is malformed HTML, nothing you can really do about it if you do not control the HTML.

Kris
  • 5,923
  • 2
  • 26
  • 44
  • Thanks for the reply. That got rid of most of the warnings, however, I am still getting this one: `Warning: DOMDocument::loadHTMLFile(): Empty string supplied as input in C:\xampp\htdocs\www\test\xpathgenius.php on line 15` Any ideas why this is? – AnchovyLegend Jun 29 '13 at 03:09
  • loadHTMLFile() takes in URL stings from an input file and I double checked that there arent any excessive new lines that might be mistaken for a URL... – AnchovyLegend Jun 29 '13 at 03:13
  • Which line is line 15? probably not the closing curly bracket. – Jens Erat Jun 29 '13 at 08:40