Solved

php Dom Document -> searching withing a child node

Posted on 2014-10-15
5
130 Views
Last Modified: 2014-11-10
Hi all,

Im currently able to get the values from a table and get the 8th item
$trainer =  $article->childNodes->item(8)->nodeValue;

Open in new window


the out put is like so G LeeR Varian

now within this item(8) of the childnode is the following

<td class="winner">
				<a href="/jockey/497-g-lee">G Lee</a><br />
				<em><a href="/trainer/3372-r-varian">R Varian </a></em>
			</td>

Open in new window


How can i do it so
If A Herf contains the value  jockey then $jockey = G Lee
If A Herf contains the value trainer then $trainer =R Varian
0
Comment
Question by:runnerjp2005
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40382586
Instead of returning the nodeValue of item[8] you should be able to look at the childNodes of item[8] which I assume is the row, and the child nodes for the tag <td> cell should also contain childNodes for the tags it con tains.  You just have to walk it down the tree until to get the level of detail you want.

Cd&
0
 

Author Comment

by:runnerjp2005
ID: 40382832
im not 100% sure what you mean.


below is my code after getting the tble i wanted:

$my_xpath_query = '//table[contains(@class, "result")]';
$result_rows = $xpath->query($my_xpath_query);
if ($result_rows->length > 0) {
    // IF WE DID GET THE PARENT'S <tr> PARENT'S <table> NODE
    $obj = $result_rows->item(0);
    foreach ($obj->childNodes as $article) {
        // set count as plsu one so i know how many times it has looped
        $count = $count + 1;
        // if this is the first loop ignor as its the table titles
        if ($count != 1) {
            // starst by getting the place and the comments
            //ceck to see if the is the evens count meaning its the horses place
            if ($count % 2 == 0) {
                $comments = "";
                $place = $article->childNodes->item(0)->nodeValue . "<br />";
            }
            //else its the comments
            else {
                $place = "";
                $comments = $article->childNodes->item(0)->nodeValue . "<br />";
            }

            $distance_bt = $article->childNodes->item(14)->nodeValue;
            $stall = $article->childNodes->item(2)->nodeValue;
            $horse = $article->childNodes->item(6)->nodeValue;
            //$age = $article->childNodes->item(6)->nodeValue;
            $weight = $article->childNodes->item(7)->nodeValue;
            //$headgear =  $article->childNodes->item(8)->nodeValue;
            //$OR = $article->childNodes->item(10)->nodeValue;??
            $trainer = $article->childNodes->item(8)->nodeValue;
            $jockey = $article->childNodes->item(16)->nodeValue;
            $odds = $article->childNodes->item(16)->nodeValue;


        }

        //need to put database add here!!!
        // echo   $comments;
        //echo $place;
        echo $trainer . "<br />";
        // $distance_bt .$stall .$weight.$headgea.$trainer.$odds .$jockey.$horse
    }

}

Open in new window



here is the  page source from which i get the table from view-source:http://www.racinguk.com/results/race/113234
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 40382916
I guess I don't understand what you are asking.

With the structure:
<td class="winner">
				<a href="/jockey/497-g-lee">G Lee</a><br />
				<em><a href="/trainer/3372-r-varian">R Varian </a></em>
			</td>

Open in new window

the node for the td has a property childNodes which is a collection of the nodes containing the value of the <a> tag for the jockey and the <em> tag which has a childNode for the <a>tag containing the trainer.

Instead of trying to scrape the page why don't you just ask the owners of that site for a feed or see if they have an API you can use.  Using scraping and script against the Nodes collection means that if they make a change in the page layout it will likely break your script.

I don't see a copyright notice on that site, but they appear to be selling subscription services, so if they become aware of the scraping, they may take steps to block you.

Cd&
0
 

Author Comment

by:runnerjp2005
ID: 40383070
saldy they dont have an api so will need to pull it from the site itself.  i can grab all the rows its just seperating the jockey and the trainer apart that im struggling with
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40385319
Separating them is just a matter of getting down to enough detail. each tag is a node within its parent element's  node collect the jockey and the trainer are each in their own <a> tag so all you need is to come down from the node for the <td>

So you do what you are doing now, and when you get to the <td> you you just look through its childNodes to find the nodes where the tagName is A and then grab the text.

Cd&
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Soap Server returning XML but < & > as &lt; / &gt; 9 45
SSRS 2016 Rendering HTML tables 3 25
Convert .csv to tab delimited text 11 32
PHP Mail error 3 26
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question