java - Get element based on parent in JSoup -
in following xml i'd able content of first title tag , not second. unfortunately code prints content of both title tags...
any 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 want title 1. i'm working under assumption parent tag of second title <source>, reason jsoup seems think <entry>
thanks!
thanks!
why don't use selector part of jsoup api? it's much easier use, it's cleaner, , i'm willing bet that's faster well. i'd use this:
//the line had document doc = jsoup.parse(feedxmlstring); //this titles elements elems = doc.select("title"); //and can proceed in various ways: string title1stway = elems.first().text(); string title2ndway = elems.get(0).text(); take here: jsoup selector api
Comments
Post a Comment