1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
public class test3 {
private static String xmlSource = "http://www.teluq.ca/site/infos/rss/communiques.php";
public static void main(String[] args) throws JDOMException, IOException {
// read the XML into a JDOM2 document.
SAXBuilder jdomBuilder = new SAXBuilder();
Document jdomDocument = jdomBuilder.build(xmlSource);
// use the default implementation
XPathFactory xFactory = XPathFactory.instance();
// System.out.println(xFactory.getClass());
// select all links
XPathExpression<Element> expr = xFactory.compile("//link", Filters.element());
List<Element> links = expr.evaluate(jdomDocument);
for (Element linkElement : links) {
System.out.println(linkElement.getValue());
Element Title = xFactory.compile("//channel/child::item/descendant::title", Filters.element()).evaluateFirst(jdomDocument);
System.out.println(Title.getValue());
}
// select all links in image element
expr = xFactory.compile("//image/link", Filters.element());
List<Element> links2 = expr.evaluate(jdomDocument);
for (Element linkElement : links2) {
System.out.println(linkElement.getValue());
}
// find the child element of channel whose name is title. find the
// descendant of item with name title.
Element firstTitle = xFactory.compile("//channel/child::item/descendant::title", Filters.element()).evaluateFirst(jdomDocument);
System.out.println(firstTitle.getValue());
}
} |
Partager