Jsoup - Parsing html child element - parsing

i have this html structure:
<div class="wrapper-a">
<li class="list"><h3>Text1</h3></li>
<li class="list"><h3>Text2</h3></li>
<li class="list"><h3>Text3</h3></li>
</div>
<div class="wrapper-b">
<li class="list"><h3>Text4</h3></li>
<li class="list"><h3>Text5</h3></li>
<li class="list"><h3>Text6</h3></li>
</div>
I need to get alls Text´s from list´s with ".wrapper-a" parent:
Elements links = doc.select("div[class=wrapper-a] > li[class=list]");
for (Element link : links)
{
Elements lists_s = link.select("h3");
String list_s = lists_s.text();
System.out.println(list_s);
}
What i expected to see:
Text1
Text2
Text3
No error occurring, but also no output printed.
Anybody could help me with this?
Greetings!

Try this selector:
Elements links = doc.select("div.wrapper-a li");
It should work.
Bytheway I think you are using an old version of JSoup, with a modern version (>= 1.7.1) your code works fine as it is.

Related

how do I access a parent attribute of a selected line item in a jquery ui menu

On my html page, I have multiple menus. When a line item is clicked, I want to get the id attribute of the parent anchor tag from the menu where the selection originates. My html and jquery code is below.
//HTML
<ul id="menuA">
<li>
Age Group
<ul>
<li name="ageGroup">18-21</li>
<li name="ageGroup">21-30</li>
<li name="ageGroup">30-40</li>
<li name="ageGroup">40-50</li>
<li name="ageGroup">50-60</li>
<li name="ageGroup">60-70</li>
<li name="ageGroup">70-80</li>
<li name="ageGroup">80-90</li>
<li name="ageGroup">90-100</li>
</ul>
</li>
</ul>
//JQUERY
$(function(){
$('#menuA').menu({
select: function(event, ui){
var choice = ui.item.text(); //get text of menu selection
//here is where I want to get the id attribute of the anchor tag. What I have isn't working. id is coming up undefined.
var id = ui.item.parents('a').attr('id');
}
});
You will find the corresponding tag id by using:
var id = ui.item.parents('li').find('a').attr('id');
Your previous attempt didn't work as parent selector was returning your list items () instead of searching for a tag in the main li.

Keep jQuery ui selectable value stored

I'm looking to select an item and I want to use this throughout a process, so the selected item needs to be stored somehow. My Next button should make it so I still have the selected item id in step2.php. I was thinking of using a POST or GET, but I was wondering if there are other possibilities using jquery/js, how should I do this?
And if it's not possible with jquery, how would I do it in php? The only option I see is using GET.
$(function() {
$("#selectable").selectable({
selected: function(event, ui) {
$(ui.selected).addClass("ui-selected").siblings().removeClass("ui-selected");
}
});
});
<ol id="selectable">
<li class="ui-widget-content" id="1">Item 1</li>
<li class="ui-widget-content" id="2">Item 2</li>
<li class="ui-widget-content" id="3">Item 3</li>
<li class="ui-widget-content" id="4">Item 4</li>
<li class="ui-widget-content" id="5">Item 5</li>
<li class="ui-widget-content" id="6">Item 6</li>
<li class="ui-widget-content" id="7">Item 7</li>
</ol>
Next
I have created a jsFiddle
Thank you for any help.
could you store the ID in script and pass down in a querystring using the anchor click event?
Something similar to the following maybe..
$('anc_id').click( function() {
//code to move to step 2...
//something like....
var step2 = 'step2-php?id=' + _ID_ ;
....
return false;
});
note: you need to modify the function above to suit your needs.
regards

Multiple Photoswipe galleries Mobile

I have multiple PhotoSwipe galleries from a list for a portfolio which work perfectly on a Desktop, but only the 1st Gallery shows its images on mobile devices, and the subsequent galleries do not. The PhotoSwipe instance instantiates and I can navigate using the arrows etc, but no images.. any ideas? (I have tried using rel="external" to no avail)
The photoswipe code:
function startSwipe(btnId)
{
var galleryName = btnId;
(function(window, PhotoSwipe)
{
var
options = {
preventHide: false,
loop: false,
captionAndToolbarShowEmptyCaptions: false,
getImageSource: function(obj){
return obj.url;
},
getImageCaption: function(obj){
return obj.caption;
}
},
instance = PhotoSwipe.attach(
[
{ url: 'http://www.example.com/gallery/'+galleryName+'/images/0.jpg'},
{ url: 'http://www.example.com/gallery/'+galleryName+'/images/1.jpg'},
{ url: 'http://www.example.com/gallery/'+galleryName+'/images/2.jpg'},
],
options
);
instance.show(0);
}(window, window.Code.PhotoSwipe));
}
and the jqm:
<div data-role="content">
<ul data-role="listview" data-theme="a" class="centerLiPort">
<li id="gal1" onClick="startSwipe(this.id)">
<div class="imgDiv"><img src="images/gal1.jpg" ></img></div>
<a>Gallery 1</a>
</li>
<li id="gal2" onClick="startSwipe(this.id)">
<div class="imgDiv"><img src="images/gal2.jpg" ></img></div>
<a>Gallery 2</a>
</li>
<li id="gal3" onClick="startSwipe(this.id)">
<div class="imgDiv" ><img src="images/gal3.jpg" ></img></div>
<a>Gallery 3</a>
</li>
</ul>
</div><!--/content-->
Well I couldn't solve this, the only work around I found was to duplicate the image galleries into the subdomain directories which really stinks, but it works. The error lies somehow in how IOS handles image linking - as it works perfectly fine on a desktop and other mobile devices.

html parsing using htmlcleaner

i want to parse this type of html using html cleaner..
<div class="result-item yt-uix-tile yt-tile-default *sr">
<div class="thumb-container">
<a href="/watch?v=NZiEqhrIL_k" class="ux-thumb-wrap contains-addto result-item-thumb">
<span class="video-thumb ux-thumb yt-thumb-default-138 ">
<span class="yt-thumb-clip">
<span class="yt-thumb-clip-inner">
<img onload="tn_load(2)" alt="Thumbnail" src="//i3.ytimg.com/vi/NZiEqhrIL_k/default.jpg" width="138" >
<span class="vertical-align"></span>
</span>
</span>
</span>
<span class="video-time">2:40</span>
in it i only want to get href ( href="/watch?v=NZiEqhrIL_k" ) value. how can i achieve it. thanks in advance.
quick and dirty, in javascript,
so for each line in your return, set thisLine:
var thisLine = "<a href=\"/watch?v=NZiEqhrIL_k\" class=\"ux-thumb-wrap contains-addto result-item-thumb\">";
then find the start of the bit you want and the end:
var startPos = thisLine.indexOf("<a href=\"/watch?");
thisLine = thisLine.substring(startPos+2);
var endPos = thisLine.indexOf("class=");
thisLine = thisLine.substring(0,endPos-1);
There's probably a 1000 ways to do this... look at the Related questions on the right side, or do a search for parse html response.

jQuery UI: sortable('toArray') returns an empty array

This has me stumped. The follow code returns ",,,,,,":
<script type="text/javascript">
$(function() {
$('#listB').sortable({
connectWith: '#listA',
update: function(event, ui) {
var result = $(this).sortable('toArray');
alert(result);
}
});
$('#listA').sortable({
connectWith: '#listB'
});
});
</script>
<div id="boxA">
<ul id="listA" class="myList">
<li value="1">Item A</li>
<li value="2">Item B</li>
<li value="3">Item C</li>
<li value="4">Item D</li>
<li value="5">Item E</li>
<li value="6">Item F</li>
<li value="7">Item G</li>
</ul>
</div>
<div id="boxB">
<ul id="listB" class="myList">
<li value="1">Item A</li>
<li value="2">Item B</li>
<li value="3">Item C</li>
<li value="4">Item D</li>
<li value="5">Item E</li>
<li value="6">Item F</li>
<li value="7">Item G</li>
</ul>
</div>
Why?! It's driving me insane! Any suggestions?
You can define which attribute to fetch like this:
var result = $(this).sortable('toArray', {attribute: 'value'});
.sortable('toArray') serializes items Ids into array, and your items have no Ids, that's why you have empty strings.
I see a few purely javascript answers. They work but beware, they may not return the items in the order that is visible on the screen. Using the code below, see jtsalva above, will return the items in the proper sorted order. This had me stumped for a while because I wanted to save the new order to a database, so I could reload the grid where someone left off.
var result = $(this).sortable('toArray', {attribute: 'value'});
I was having this issue as well except i did have id's on my elements, jQuery's sortable('toArray') was very hit an miss on return the ids, however you can grab them in javascript using this:
function getSortOrder() {
var children = document.getElementById('sortedElement').childNodes;
var sort = "";
for (x in children) {
sort = sort + children[x].id + ",";
}
return sort;
}
This of course returns the ID's in a comma delimited string but you can return the array.
I'm sure there's a better way to solve this problem, this is just the solution i found.
To use another attribute you can do this:
$('#element').sortable('toArray' {attribute: 'value'})
This will make it so it now uses the attribute 'value' from your code.
Documentation on Sortable toArray method
If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "set_number" For example, a 3 element list with id attributes "foo_1", "foo_5", "foo_2" will serialize to "foo[]=1&foo[]=5&foo[]=2". You can use an underscore, equal sign or hyphen to separate the set and number. For example "foo=1", "foo-1", and "foo_1" all serialize to "foo[]=1".
jq sortable reference
$('.sortable').sortable('toArray'); will only parse the first element of the class sortable. You can parse all elements by using each:
$('.sortable').each(function(){
result.push($(this).sortable('toArray'));
})

Resources