In the following XML I’d like to be able to get the content of the first title tag and not the second. Unfortunately the code prints the content of both title tags…
Any help would be great thanks!
String feedXMLString = "<entry><title>title 1</title><source><title>title 2</title></source></entry>";
Document feedXML = Jsoup.parse(feedXMLString);
NodeTraversor feedXMLTraversor = new NodeTraversor(new NodeVisitor() {
@Override
public void tail(Node node, int depth) {
if (node instanceof Element) {
String tagName = ((Element) node).tagName();
String parentTagName = ((Element) node).parent().tagName();
if (tagName.equals("title")) {
if (parentTagName.equals("entry")) {
String title = ((Element) node).ownText();
System.out.println(title);
}
}
}
}
@Override
public void head(Node node, int depth) {
}
});
feedXMLTraversor.traverse(feedXML.body());
Output is
title 1
title 2
I just want it to be title 1. I’m working under the assumption that the parent tag of the second title is <source>, but for some reason JSoup seems to think that it is <entry>
Thanks!
Thanks!
Why don’t you use the selector part of the Jsoup API? It’s much much easier to use, it’s cleaner, and I’m willing to bet that’s faster as well. What I’d personally use is this:
Take a look here: Jsoup Selector API