tags:

views:

184

answers:

1

I have a table with the following structure. I cannot seem to get the data I want.

<table class="gsborder" cellspacing="0" cellpadding="2" rules="cols" border="1" id="d00">
 <tr class="gridItem">
  <td>Code</td><td>0adf</td>
 </tr><tr class="AltItem">
  <td>CompanyName</td><td>Some Company</td>
 </tr><tr class="Item">
  <td>Owner</td><td>Jim Jim</td>
 </tr><tr class="AltItem">
  <td>DivisionName</td><td>&nbsp;</td>
 </tr><tr class="Item">
  <td>AddressLine1</td><td>9314 W. SPRING ST.</td>
 </tr>
</table>

This table is of course nested within another table within the page. How can I use DomDocument for example to refer to "Code" and "0adf" as a key value pair? They actually don't need to be in a key value pair but I should be able to call them each separately.

EDIT:

Using PHP Simple HTML, I was able to extract the data I needed using this:

  $foo = $html->getElementById("d00")->childNodes(1)->childNodes(1);

The problem with this though is that I am getting the two <td></td> tags with my data. Is there a way to only grab the raw data without the tags?

Also, is this the right way to get my data out of this table?

A: 

If you're not dead set on using DOMDocument, try using the PHP Simple HTML DOM Parser. This has the benefit of allowing you to parse HTML which is not valid XML as well as providing a nicer interface to the parsed document.

You could write something like:

$html = str_get_html(...);
foreach($html->find('tr') as $tr) 
{
  print 'First td: ' . $tr->find('td', 0)->plaintext;
  print 'Second td: ' . $tr->find('td', 1)->plaintext;
}
Andy
Hi Andrew, I'm not set on it at all actually. I'm only using this to transfer over some data. So, the way I am working this now is importing the document with simplexml. I'm assuming that the str_get_html() takes care of grabbing the contents, yes?
So str_get_html() parses HTML data from a string, you can use file_get_html() to read from a file or URL and perhaps avoid using simplexml altogether?
Andy
Yep.. This will do exactly what I need. Thanks Andrew!
I just tossed simplexml out. I like this much better. :) This is certainly more straightforward than simplexml and has better documentation.
Andrew, are you still around? I cannot get this working... I was wondering of you wouldn't mind having a look at what I have.
Sure, what have you got?
Andy
Ah, great! Thanks Andrew.. I'll post it above in my original post. I'm not sure if this is the "optimal" way of doing it but it semi works.
Or start another question. You might get help from others as well if you do that.
Andy
Ok, thanks Andrew. I will do that now.