Iterating Hashmap in jsp and Showing Data in Dynamically Generated Table - hashmap

I am having hashmap in jsp.
I want to print all the values that hashmap contains in a table. The table should generated dynamically and after 3 columns next values should be displayed in next row and so on..Using jstl or any tag library.
So if hashmap contains 6 values then HTML generated should be:
<table>
<tr>
<td> val1 </td>
<td> val2 </td>
<td> val3 </td>
</tr>
<tr>
<td> val1 </td>
<td> val2 </td>
<td> val3 </td>
</tr>
</table>
If hashmap contains 8 values then HTML generated should be:
<table>
<tr>
<td> val1 </td>
<td> val2 </td>
<td> val3 </td>
</tr>
<tr>
<td> val4 </td>
<td> val5 </td>
<td> val6 </td>
</tr>
<tr>
<td> val7 </td>
<td> val8 </td>
</tr>
</table>
So as per number of values the number of rows should get added to table. How do I do that in jsp... either using jstl tags or any custom tag library?

Simply add the following java code in your jsp
<table>
<%
int count=0;
Iterator itr = yourMap.iterator();
while(itr.hasNext()) {
String key = itr.next();
if(count ==0) {
%>
<tr>
<% } %>
<td><% =yourMap.get(key) %></td>
<%
count ++;
if(count ==3 ) {
count =0;
%>
</tr>
<%>
}
</table>
Here I am assuing your map is <String,String> and please import relevant classes for map and iterator. Any JSP tutorial can tell you how o do that

Related

How do I parse this html with Python lxml & xpath that finds the parent table of a specific span id?

Here is the HTML I don't have any control over. This is condensed HTML of the real page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Little League</title>
</head>
<body>
<table>
<span>lot of unrelated text</span>
</table>
<table>
<span>lot of unrelated text</span>
</table>
<table>
<span>lot of unrelated text</span>
</table>
<table>
<tbody>
<tr>
<td class="rightTD">
<p>
<span id="teams_players">Player Teams</span>
</p>
</td>
</tr>
<tr>
<td>
<table border="1" cellspacing="0" cellpadding="0" class="tableBorder table table-bordered" width="100%">
<tbody>
<tr>
<td>
<table border="0" width="100%" class="tableData">
<tbody>
<tr id="team_listings">
<td colspan="3">Team Listings
<br>
<br>
</td>
</tr>
<tr>
<td>(a) </td>
<td colspan="2">Team Name </td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">Foxes</span>
</td>
</tr>
<tr>
<td>(b) </td>
<td colspan="2">Team Rank</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">1</span>
</td>
</tr>
<tr>
<td>(c) </td>
<td colspan="2">Team Location
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<table width="100%">
<tbody>
<tr>
<td>City:
<br>
<span class="blue_color">Tualatin</span>
</td>
<td>State:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">Oregon</span>
</td>
<td>Country:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">United States</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<br>
<table border="1" cellspacing="0" cellpadding="0" class="tableBorder table table-bordered" width="100%">
<tbody>
<tr>
<td>
<table border="0" width="100%" class="tableData">
<tbody>
<tr>
<td>(a) </td>
<td colspan="2">Team Name </td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">Tigers</span>
</td>
</tr>
<tr>
<td>(b) </td>
<td colspan="2">Team Rank</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<span class="blue_color">3</span>
</td>
</tr>
<tr>
<td>(c) </td>
<td colspan="2">Team Location
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<table width="100%">
<tbody>
<tr>
<td>City:
<br>
<span class="blue_color">Tigard</span>
</td>
<td>State:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">Oregon</span>
</td>
<td>Country:
<br>
<span class="blue_colorLined"></span>
<br>
<span class="blue_color">United States</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
I am trying to get to the table tag immediately preceding the span tag with id team_players.
I tried these but failed -
//table/span[#id="teams_players"]
ancestor::table[span[#id="teams_players"][position() = 1]]
This works but is not elegant and I prefer not to hardcode it -
//span[#id="teams_players"]/../../../../..
While //table[#class="tableData"] this might seem like it should work, there are many such tables in the HTML that has the same class with unrelated data. So this is ruled out.
Here is the code so far with my attempts (definitely not efficient, once I find a way of fetching both tables, I plan on looping through them to extract the data -
def parse_team():
# team data structure
teams = []
team_dict = { 'team': '', 'rank': '', 'location': { 'city': '', 'state': '', 'country': '' } }
filename = f'team.html'
f = open(filename, encoding="utf8").read()
parser = etree.HTMLParser()
tree = etree.parse(StringIO(f), parser)
# fetch the table dom and parse each team table
# fetch the parent table that contains teams_players span id
team_tables = tree.xpath('ancestor::table[span[#id="teams_players"][position() = 1]]')
print(team_tables)
root_tables = tree.xpath('//table/span[#id="teams_players"]')
print("root tables", root_tables)
# this provides each team table but in full html, the same class is being used for other unrelated data
name = tree.xpath('//table[#class="tableData"]')
print(name)
eachvaltr = name[0].xpath('.//tr')
teamname = name[0].xpath('.//td[contains(text(),"Team Name")]//parent::tr/following-sibling::tr[1]//span[#class="blue_color"]/text()')
print("teamname", teamname)
teamrank = name[0].xpath(
'.//td[contains(text(),"Team Rank")]//parent::tr/following-sibling::tr[1]//span[#class="blue_color"]/text()')
print("teamrank", teamrank)
city = name[0].xpath(
'.//td[contains(text(),"City")]//span[#class="blue_color"]/text()')
state = name[0].xpath(
'.//td[contains(text(),"State")]//span[#class="blue_color"]/text()')
country = name[0].xpath(
'.//td[contains(text(),"Country")]//span[#class="blue_color"]/text()')
print(city[0], state[0], country[0])
team_dict['team'] = teamname
team_dict['rank'] = teamrank
team_dict['location']['city'] = city[0]
team_dict['location']['state'] = state[0]
team_dict['location']['country'] = country[0]
print(team_dict)
Desired output is a list of teams where each team is a dict.
[{'team': ['Foxes'], 'rank': ['1'], 'location': {'city': 'Tualatin', 'state': 'Oregon', 'country': 'United States'}}]
//table[.//span[#id="teams_players"]]
or
//span[#id="teams_players"]/ancestor::table

Get tr having specific th value from the table having an id using Xpath

I am using python3.6 with XPath library. Crawling inside the table gives me an empty list. And need to crawl to specific th.
My tr contents are dynamically generated. I need to crawl to tr which has a specific th value. Example In HTML code, the Rank appears in the second tr but it can appear in anywhere in tr. It doesn't have a specific index. Need to get the href from the tr having the Rank th.
My html file:
<tbody>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Product Number
</th>
<td class="a-size-base">
B003NR57BY
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Rank
</th>
<td>
<span>
<span>#3 in Computer Mice</span>
<br>
</span>
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">
Created Date
</th>
<td class="a-size-base">
June 7, 2010
</td>
</tr>
</tbody>
</table>
Python code:
listings_details = parser.xpath(XPATH_PRODUCT_DETAILS)
for row in listings_details:
th = row.xpath("./th/text()")
if th[0].strip() == 'Rank':
categories = row.xpath("./td/span/span//text()")
qid_url= row.xpath("./td/span/span//#href")
I expect the output to be
Rank: 3,
url : /gp/bestsellers/pc/11036491/ref=pd_zg_hrsr_pc_1_1_last,
category: Computer Mice
Need to get the href from the tr having the Rank th.
Use:
/table/tbody/tr[normalize-space(th)='Rank']/td//a/#href
Note: this works for your provided fragment (now well-formed). You need to add later a context for selecting the table element.
<table>
<tbody>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">Product Number</th>
<td class="a-size-base">B003NR57BY</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">Rank</th>
<td>
<span>
<span>#3 in
Computer Mice
</span>
<br/>
</span>
</td>
</tr>
<tr>
<th class="a-color-secondary a-size-base prodDetSectionEntry">Created Date</th>
<td class="a-size-base">June 7, 2010</td>
</tr>
</tbody>
</table>
Test in http://www.xpathtester.com/xpath/53808ee94dfbc5b38f12791cf857ffb9

bash shell script that takes two columns of data from text file and uses a for loop with two variables

I am trying to take a text file with two columns of data and loop over each line applying html table tags to each column and write to another file e.g.
example.txt
1 pool1
2 pool2
3 pool3
This is what I have so far.
for line in $(cat example.txt); do
count=$(echo $line | awk -F " " '{print$1}')
pool=$(echo $line | awk -F " " '{print$2}')
printf "\t<tr>\n\t <td>%s</td> <td>%s</td>\n\t</tr>\n" $count $pool >> example_info.html
done
Current output:
<tr>
<td>1</td>
<td></td>
</tr>
<tr>
<td>pool1</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td></td>
</tr>
<tr>
<td>poo2</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td></td>
</tr>
<tr>
<td>poo3</td>
<td></td>
</tr>
Desired output:
<tr>
<td> 1 </td>
<td> pool1 </td>
</tr>
<tr>
<td> 2 </td>
<td> pool2 </td>
</tr>
<tr>
<td> 3 </td>
<td> pool3 </td>
</tr>
Can anyone point out where my for loop is messed up?
You need a while read loop:
while read count pool
do
printf "\t<tr>\n\t<td>%s</td><td>%s</td>\n\t</tr>\n" $count $pool >> example_info.html
done < example.txt
You should read up on the Bash read command and decide whether you need the -r option or not.
I assume there is other code to print the HTML heading and body and table heading and ending tags, etc.
Try this method also
sed 's/\(.*\) \(.*\)/<tr>\n <td> \1 <\/td>\n <td> \2 <\/td>\n<\/tr>/' FileName
Output:
<tr>
<td> 1 </td>
<td> pool1 </td>
</tr>
<tr>
<td> 2 </td>
<td> pool2 </td>
</tr>
<tr>
<td> 3 </td>
<td> pool3 </td>
</tr>
This is more suited to awk:
awk '{print "<tr>"; for (i=1; i<=NF; i++) print "\t<td> " $i " </td>"; print "</tr>"}' file
<tr>
<td> 1 </td>
<td> pool1 </td>
</tr>
<tr>
<td> 2 </td>
<td> pool2 </td>
</tr>
<tr>
<td> 3 </td>
<td> pool3 </td>
</tr>

JSF UIData doesn't render a UIColumn children

I try understand how JSF UIData () works programaticaly in JSF. I have following code:
UIInput input = new UIInput();
UIInput input2 = new UIInput();
UIOutput header = new UIOutput();
header.setValue("Header");
UIColumn column = new UIColumn();
column.setHeader(header);
column.getChildren().add(input);
column.getChildren().add(input2);
UIData table = new UIData();
table.getChildren().add(column);
this code render html like:
<table>
<thead>
<tr>
<th scope="col">Header</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
where is my two inputs?? i expect something like:
.....
<tbody>
<tr>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>
<input type="text" />
</td>
</tr>
</tbody>
......
i know what i do wrong. I have to set a collection of UIData for rows of table. When i set
UIData table = new UIData();
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
table.setValue(list);
than i get two rows with my inputs

Search for a value and move it to another spreadsheet

I have a little problem using Excel 2010. The problem is simple but I think I start in a wrong direction. Let's say I have a document with the following structure:
<table>
<thead>
<th> Col A</th>
<th> Col B</th>
<th> Col C</th>
</thead>
<tr>
<td> NONE</td>
<td> TEST</td>
<td> NONE</td>
<td> TEST2</td>
<td> TEST3</td>
<td> TEST4</td>
</tr>
<tr>
<td> NONE</td>
<td> TEST</td>
<td> NONE</td>
<td> TEST2</td>
<td> TEST3</td>
<td> TEST4</td>
</tr>
<tr>
<td> TEST</td>
<td> TEST</td>
<td> NONE</td>
<td> TEST2</td>
<td> TEST3</td>
<td> TEST4</td>
</tr>
<tbody>
</tbody>
</table>
I want to search for NONE and move it to another spreadsheet. I want to do this with formula. I tried with SEACH but I don't know how to move it to another spreadsheet.

Resources