如何在页面上使用DomXPath获取第二个元素DOM?

时间:2013-07-15 20:15:53

标签: php xpath domxpath

有HTML结构,我需要获取TD的每个TR文本值

<table class="dataTable all_clients" style="width:100%;" cellpadding="0" cellspacing="0" border="0">
      <tbody>
        <tr>
          <td width="15px"></td>
          <td width="25%">
            <strong>Комиссии за сутки</strong>
          </td>
          <td></td>
        </tr>
        <tr>
          <td colspan="5" style="height:5px" class="line"></td>
        </tr>
        <tr>
          <td></td>
          <td style="width:200px;">
            514596        
          </td>
          <td style="text-align:right;width:70px;" class="redData">
            0.00        
          </td>
          <td class="redData">
            USD        
          </td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td style="width:200px;">
            514597        
          </td>
          <td style="text-align:right;width:70px;" class="redData">
            0.00        
          </td>
          <td class="redData">
            RUR        
          </td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td style="width:200px;">
            514598        
          </td>
          <td style="text-align:right;width:70px;" class="redData">
            0.00        
          </td>
          <td class="redData">
            EUR        
          </td>
          <td></td>
        </tr>
      </tbody>
    </table>

PHP代码:在我得到所有内容后,我需要在TD获取文本值

<? libxml_use_internal_errors(true);
        $dom = new domDocument('1.0', 'UTF-8');
        $dom->strictErrorChecking=false;
        $dom->recover = true;
        @$dom->loadHTML($data);
        $xpath = new DomXPath($dom);

        $elements = $xpath->query("//table[@class='dataTable all_clients']//tr");
        $result = array();
        $title = array();

        if (!is_null($elements)) {

            foreach ($elements as $element) {

                  $titles = $xpath->query('//strong', $element);       //Get titles
                  $numbers = $xpath->query('//td[1]', $element);       //Get number
                  $numbers_flt = $xpath->query('//td[2]', $element);   //Get number 0.00
                  $symbols = $xpath->query('//td[3]', $element);       //Get Symbol

                  for( $i = 0; $i < count($numbers); $i++) {

                        $result['numbers'][$i] = $numbers->nodeValue;
                        $result['numbers_flt'][$i] = $numbers_flt->nodeValue;
                        $result['symbols'][$i] = $symbols->nodeValue;

                  }

                  foreach ($titles as $value) {
                    $title[] = $value->nodeValue;
                  }
            }
        }

        var_dump($result);
            }?>

所以,我需要格式数组$ result,但仍然没有找到解决方法

0 个答案:

没有答案