Jump to content

Getting data from nested tables in string.


pwntastic

Recommended Posts

Hello, I have some html in a string from an AJAX response.  I'm currently trying to filter certain information.  I tried to extract rows from the table with a regular expression but the problem is the table is made something like this: 

 

<table>

     <tbody>

          <tr>

               <td>DATA</td>

               <td>DATA</td>

               <td><table><tbody><tr><td>DATA</td></tr><tr><td>DATA</td></tr></tbody></table></td>

          </tr>

 

My regular expression is: <tr>[^()]*<\/tr>

This gets the first tr through like the second </tr> in the nested table though.  Does anyone have any suggestions with the regular expression or even a better way to go about this?  Thanks in advance.

even a better way to go about this?

 

 

if this is your application, the data should be output in a format that supports how you are using the data, not make it harder, requiring more processing before you can even use the data. you should output the data as a JSON object with key/values that let you directly access the data (you should only output the data you want as well.)

I'm not too familiar with this.  I tried something simple like this to try to get the hang of it but I get weird results. I've been googling this around and have come across similar for loops to put the data in.

 

<html>    
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            window.onload = function() {
                var data = document.body.innerHTML;
                var div = document.createElement('div');
                div.innerHTML = data;
 
                var matches = new Array();                
                for (var i = 0; i < data.length; i++) {                                       
                        matches.push(data[i]);                    
                }
                
 
            }
 
        </script>        
    </head>
    <body id="body_content">
        <div>
            hello
            <div>
                hello
                <div>
                    hello
                </div>
            </div>
        </div>                    
    </body>
</html>

 

in this code I get data.length = 203

What data EXACTLY are you wanting from that content? All you said is you want to "filter certain information". Also, I just realized you are doing this in JS, not PHP. So, you can try to build the functionality in JS OR you could do a 2nd AJAX call and pass the content to a page you create that will parse the data from the first call and send it back in the format you need.

Yes, the thing is I have a table with prices, item names, item options, and other item related info from the ajax request.  I'm trying to get this information, and display it in a div but I can't display it the way it is in the table.  So I would like to extract the information, and place it in my own layout.  The code displayed last by me, is only a demonstration of one way I was trying to do this but I have failed miserably. 

Hello, I figured out I can use $.parseHTML() or DOMParser().  I can't use $.parseHTML because the code I'm working on uses jquery 1.7.2 (using the current vers. causes problems with some other parts of the page).  For some reason when I use DOMParser, seems to work on chrome and fire fox but not on ie.  I've tried a simple example though in ie with the DOMParser and that worked well.  Why would it not work on ie? I believe it should work in ie8+.  

 

 

Also, another problem I found is that ff and ie read my regular expressions differently.  My regular expression is like so: 

 

\<tbody\>[\s\<\>a-zA-Z\=\"\:\/\d\.\-\_\&\;\[\]\,\#\(\)\'\?$$]*\<\/tbody\>/g

 

In chrome this expression returns all the code between tbody tags.  In ff and ie it only returns the beginning and ending tbody tags and some space in between.  

Why is this?

I've figured out the problem.  Was pretty silly on my part.  It wasn't displaying information on other browsers because there was no information in the cart for those browsers.  :facepalm:

I apologize for this and I'm thankful for those who attempted to help.  

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.