Jump to content

Getting data from nested tables in string.


pwntastic
Go to solution Solved by 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.

Link to comment
Share on other sites

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.)

Edited by mac_gyver
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Edited by Psycho
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.