php - XPath. Determining the row of a scraped table -


i having problem creating insert statement using xpath. here table:

<table>   <tr>     <td class="messagecellbody">row1 cell1</td>     <td class="messagecellbody">row1 cell2</td>     <td class="messagecellbody">row1 cell3</td>   </tr>   <tr>     <td class="messagecellbody">row2 cell1</td>     <td class="messagecellbody">row2 cell2</td>     <td class="messagecellbody">row2 cell3</td>   </tr> </table> 

and output ideally this:

insert database_table (column1, column2, column3) values ('row1 cell1', 'row1 cell2', 'row1 cell3'); insert database_table (column1, column2, column3) values ('row2 cell1', 'row2 cell2', 'row2 cell3');

the code using this:

$my_xpath_query = "//table//td[contains(@class, 'messagecellbody')]"; $result_rows = $xpath->query($my_xpath_query);  foreach ($result_rows $result_object) {     echo "'" . $result_object->nodevalue . "'"; } 

this finds each correctly, output cell contents of 6 cells. don't know how break on each row (so can echo 'insert database_table' etc.)

if insert parameter, can filter column number e.g. following give me first column

$my_xpath_query = "//table//td[contains(@class, 'messagecellbody')][1]"; 

but problem remains of not knowing when row ends , 1 starts. without this, difficult create insert statement.

maybe try instead getting tr rows individual objects in xpath search. there tr elements children don't wish include?

then able child nodes of objects (i.e. td elements you're looking for).

something like:

 $my_xpath_query = "//table//tr"; $result_rows = $xpath->query($my_xpath_query);  foreach ($result_rows $result_object) {     foreach($result_object->childnodes $td) {          echo "'" . $td->nodevalue . "'";     }   } 

i haven't tested - think might solve problem.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -