40569

Parsing table html with Jsoup

Question:

I'm try to parsing this table:

<table border="1" align="center" cellpadding="5" width="95%"> <tbody> <tr> <td colspan="2" align="center"> <b> <i> Test </i> </b> </td> <td> <b> <i> Result </i> </b> </td> <td> <b> <i> Credit </i> </b> </td> <td> <b> <i> Data </i> </b> </td> <td> <b> <i> A/A </i> </b> </td> <td> <b> <i> Other data </i> </b> </td> <td> <b> <i> A/A rif. </i> </b> </td> </tr> <tr> <td> A000211 </td> <td nowrap=""> Physic </td> <td align="center"> - </td> <td align="center"> 6 </td> <td align="center"> - </td> <td align="center"> 2008/2009 </td> <td> something </td> <td align="center"> 2007/2008 </td> </tr> <tr> <td> 0065057 </td> <td nowrap=""> Math </td> <td align="center"> - </td> <td align="center"> 6 </td> <td align="center"> - </td> <td align="center"> 2008/2009 </td> <td> samething </td> <td align="center"> 2008/2009 </td> </tr> <tr>

In java i have this, for now:

Document doc = Jsoup.parse(url); Elements tables = doc.getElementsByTag("table");

I try to put this data in JsonObject i must iterate this tables? or there is a simple way?

Answer1:

i solved with:

Document doc = Jsoup.parse(url); JSONObject jsonObject = new JSONObject(); JSONArray list = new JSONArray(); Element rows = doc.getElementsByTag("table tr"); for(Element row : rows) { String Test = row.getElementsByTag("td").get(1).text(); String Result = row.getElementsByTag("td").get(2).text(); String Credit = row.getElementsByTag("td").get(3).text(); jsonObject.put("Test", Test); jsonObject.put("Result", Result); jsonObject.put("Credit", Credit); }

Recommend

  • Scala regex match and split
  • Spl_autoload_register() not working on server
  • How do I hide the last box on append?
  • Change how [DataType.Currency] renders html
  • Bootstrap 3 truncate text in column on condensed table adding padding
  • How to get ZWJ (Zero-width Joiner) entity to work with Japanese characters?
  • flexbox margin collapsing between children [duplicate]
  • Extra space in a element horizontally scrolled element with relatively positioned items
  • Content within div won't fill available space without specifying height
  • Toggle dropdown mini-cart when product added to basket
  • How to get bluetooth mac address from local pc?
  • How do I make new messages appear on left/right depending on who sent them? (No Float)
  • The script does not work in IE. How can I fix it?
  • How to use Swing Timer ActionListener
  • jQuery: remove the closest with a dynamically added button
  • Bootstrap-datetimepicker - dynamically add shows up in wrong area
  • When using `nowrap` within a flex row, avoid pushing the last elements off the screen
  • Where is this gap/margin coming from?
  • Creating a table in “MSWord” document out of a text in a .txt file
  • Submitting two different forms with an external Submit button not working properly
  • In struts1.2 how to populate dropdown according to database value
  • RESTful compliant design
  • D3 get axis values on zoom event
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • php design question - will a Helper help here?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Buffer size for converting unsigned long to string
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app