Jsoup: Selecting HTML Between Different Classes
I'm trying to do something similar to: Jsoup: How to get all html between 2 header tags
However, it seems my c开发者_StackOverflow中文版ode is avoiding plain text. The site I'm parsing has code setup in such a way:
div class = "quoted-message" Response. Can contain images, text, etc. div class = "quoted-message" Another response to another quoted message
Code Snippet used to handle the actual messages:
Element quote = msg.select(".quoted-message").first();
Boolean hasQuote = false;
Elements siblings = null;
siblings = quote.siblingElements();
createQuotePost(quote);
List<Element> elementsBetween = new ArrayList<Element>();
for (int i = 1; i < siblings.size(); i++) {
Element sibling = siblings.get(i);
if (! "div.quoted-message".equals(sibling.tagName())) {
elementsBetween.add(sibling);
}
else {
Log.v("location", "Clear and Process");
processElementsBetween(elementsBetween);
elementsBetween.clear();
}
}
if (! elementsBetween.isEmpty())
processElementsBetween(elementsBetween);
This, however, does not seem to work as I want it to. The responses to the code do not have any special formatting to them (ie: sitting in a p tag). Using a bit of logging, I can see they aren't getting put into Elements siblings. Siblings seems to just include line breaks and such.
Note: I've only tested this on small posts (simple one liners) to save on sifting through long pages of printouts.
Any suggestions on what to do?
EDIT: Here is the HTML code snippet between 2 quoted-message divs:
MESSAGE TO BE QUOTED
</div>
<br />
<br />
Hello quoted message
<br />
I am a response
<br />
<br />
<div class="quoted-message">
Think one of the problems is you're asking for Elements and not Nodes. Text nodes are Nodes and not Elements.
Try this:
package grimbo.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
public class StackOverflow {
public static void main(String[] args) {
String html = "<div class=quoted-message>message-1</div>\n <br />\n <br />\n Hello quoted message\n <br />\n I am a response\n <br />\n <br />\n";
html += "<div class=quoted-message>message-2</div>\n <br />\n <br />\n Hello quoted message\n <br />\n I am a response\n <br />\n <br />\n";
Document doc = Jsoup.parse(html);
handleQuotedMessages(doc.select(".quoted-message"));
}
private static void handleQuotedMessages(Elements quotedMessages) {
Element firstQuotedMessage = quotedMessages.first();
List<Node> siblings = firstQuotedMessage.siblingNodes();
List<Node> elementsBetween = new ArrayList<Node>();
Element currentQuotedMessage = firstQuotedMessage;
for (int i = 1; i < siblings.size(); i++) {
Node sibling = siblings.get(i);
// see if this Node is a quoted message
if (!isQuotedMessage(sibling)) {
elementsBetween.add(sibling);
} else {
createQuotePost(currentQuotedMessage, elementsBetween);
currentQuotedMessage = (Element) sibling;
elementsBetween.clear();
}
}
if (!elementsBetween.isEmpty()) {
createQuotePost(currentQuotedMessage, elementsBetween);
}
}
private static boolean isQuotedMessage(Node node) {
if (node instanceof Element) {
Element el = (Element) node;
return "div".equals(el.tagName()) && el.hasClass("quoted-message");
}
return false;
}
private static List<Element> filterElements(String tagName, List<Node> nodes) {
List<Element> els = new ArrayList<Element>();
for (Iterator<Node> it = nodes.iterator(); it.hasNext();) {
Node n = it.next();
if (n instanceof Element) {
Element el = (Element) n;
if (el.tagName().equals(tagName)) {
els.add(el);
}
}
}
return els;
}
private static void createQuotePost(Element quote, List<Node> elementsBetween) {
System.out.println("createQuotePost: " + quote);
System.out.println("createQuotePost: " + elementsBetween);
List<Element> imgs = filterElements("img", elementsBetween);
// handle imgs
}
}
精彩评论