2

I would like to get a complete html table having id = 'myid' from a given url using php domddocument and print it to our web page, How can i do this ?

I am trying with below code to get table but i cant getting trs(table rows) and tds(table data) and other inner html.

$xml = new DOMDocument();

@$xml->loadHTMLFile($url);
foreach($xml->getElementById('myid') as $table)
{
  // now how to get tr and td and other element ?
  // i am getting other element like :-
   $links = $table->getElementsByTagName('a');
   foreach($links as $innerAnchor)
  {
    //doing something with anchor tag...
   }

}

Need help.

I am new in php domddocument.

Thanks a lot.

hakre
  • 193,403
  • 52
  • 435
  • 836
Bajrang
  • 8,361
  • 4
  • 27
  • 40
  • Take the @ off `@$xml` while you're debugging, then at least you will be able to see the errors... what you're doing in general looks about right, but it's late for me and I've had a few drinks so I'd have to have a better look in the morning when I've sober... – Endophage Sep 15 '11 at 10:12
  • @ajreal, The html is given from the url passed in $xml->loadHTMLFile function, which can be anything but would be a valid url. – Bajrang Sep 15 '11 at 10:15
  • getElementById returns only one element as I suppose. There's no need in the first "foreach" – mishau Sep 15 '11 at 11:50
  • From the php manual about the getElementById function: For this function to work, you will need either to set some ID attributes with DOMElement::setIdAttribute or a DTD which defines an attribute to be of type ID. In the later case, you will need to validate your document with DOMDocument::validate or DOMDocument->validateOnParse before using this function. – mishau Sep 15 '11 at 12:11
  • Sibling Question: [print html table using php dom document](http://stackoverflow.com/q/7441042/367456) – hakre Jun 23 '13 at 22:44

1 Answers1

9

As I commented it's better not to use getElementById. Better analyze my test sample; it works

$html = "<table ID='myid'><tr><td>1</td><td>2</td></tr><tr><td>4</td><td>5</td></tr><tr><td>7</td><td>8</td></tr></table>";

$xml = new DOMDocument();
$xml->validateOnParse = true;
$xml->loadHTML($html);

$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[@id='myid']")->item(0);

// for printing the whole html table just type: print $xml->saveXML($table); 

$rows = $table->getElementsByTagName("tr");

foreach ($rows as $row) {
  $cells = $row -> getElementsByTagName('td');
  foreach ($cells as $cell) {
    print $cell->nodeValue; // print cells' content as 124578
  }
}
mishau
  • 582
  • 4
  • 11
  • In theory this should work, but you can't possibly know the depth of element table is exactly located at second level (like your example). – ajreal Sep 15 '11 at 15:37
  • No, //* ignores the depth. This string gives the same result:[code]"
    12
    45
    78
    "
    – mishau Sep 15 '11 at 20:21
  • @mishau, But i want to print complete html table in my web page with all contents likes anchor, img, inner table tr tds, p etc. as it is. And $cell->nodeValue printing only text not all other elements. – Bajrang Sep 16 '11 at 05:12
  • It's even simplier: after you've got $table put this line `print $xml->saveXML($table);` – mishau Sep 16 '11 at 10:39
  • Fantastic Answer! You have solved my problem within few seconds. Thanks – Junaid Usmani Mar 05 '20 at 15:48