0

I have some (xyz)html files in my local system which is having some integer value in it. I have created one(abc) html file having main summary of those files. I want to show that integer value in my html file. How can i parse through xyz html files and store into my abc html file?

<table class="log" border="0" cellpadding="5" cellspacing="2" width="100%">
<tr>
<th>Files</th><th>Errors</th>
</tr>
<tr class="a">
<td>225</td><td>2294</td>
</tr>
</table>

This is a content i have in my xyz html files.. and i need values 225 and 2294 to be extracted from here.

jasmeet
  • 31
  • 4

2 Answers2

2

You can make ajax request from where you want to show value. by parsing html you can get value of specific field

$.ajax({
    url: "http://test.com/page1.html",
    type: "GET",
    dataType: "html",
    success: function(data) {
        var html = $.parseHTML(data);
        var first = $(html).closest('.log').find('.a').find('td:eq(0)').html();
        var second = $(html).closest('.log').find('.a').find('td:eq(1)').html();

        alert(first+" "+ second);
    }
});

See Sample DEMO of parsing html as you want

Manwal
  • 23,450
  • 12
  • 63
  • 93
  • Hi Manwal, thnx but the problem is i cannot edit those files as they are being generated by server after some execution at back end. I have only access to parse those files. I just want to read the value from there – jasmeet Oct 01 '14 at 07:50
  • No problem @jasmeet you don't have to edit them you can parse it. You can check where that value exists. Could you provide me some example where that value in html? like `your_value`. – Manwal Oct 01 '14 at 09:05
  • I have added the html code above in my questions. Please refer it. – jasmeet Oct 01 '14 at 09:11
0

If you're looking to parse the HTML code you have, you can do that with several server-side scripting languages and also with JavaScript.

Here's a solution with JS:

<!DOCTYPE html>
<html lang="en">
    <head>
        <script type="text/javascript">
            function loadPage(href){
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open("GET", href, false);
                xmlhttp.send();
                return xmlhttp.responseText;
            }
            function parseHTML(url){
                var my_arr = [];
                var el = document.createElement( 'div' );
                el.innerHTML = loadPage(url);
                var table_row = el.document.getElementsByClassName("a");
                for(var i=0; i<x.length; i++){
                    var new_arr = new Array();
                    my_arr.push(new_arr);
                    var r = x[i].getElementsByTagName('td');
                    for(var j=0; j<r.length; j++){
                        var new_val = r[j].innerHTML;
                        my_arr[i].push(new_val);
                    }
                }
                return my_arr;
            }
        </script>
    </head>

    <body onload="parseHTML('my_url_path');">

    </body>

</html>

Here's a solution with PHP and SimpleHTMLDOM:

<?php
   $str = '<table class="log" border="0" cellpadding="5" cellspacing="2" width="100%"><tr><th>Files</th><th>Errors</th></tr><tr class="a"><td>225</td><td>2294</td></tr></table>';
   $html = new simple_html_dom();
    // Load from a string
    $html->load($str);

    $arr = array();
    $iterator = 0;
    // Loop over all the <tr> with a class ".a"
    $table_rows = $html->find("tr.a");
    foreach($table_rows as $tr){
        $arr[$iterator] = array();
        // Loop over all the <td> inside a <tr> with a class ".a"
        $table_divisions = $tr->find("td");
        foreach($table_divisions as $td){
            $arr[$iterator][] = $td->innertext;
        }
        $iterator++;
    }
?>

You need to download the library:

http://simplehtmldom.sourceforge.net/manual.htm

http://sourceforge.net/projects/simplehtmldom/files/

Keep in mind that you need PHP 5.0+

Wissam El-Kik
  • 2,469
  • 1
  • 17
  • 21