Skip to content
  • jsoup
  • News
  • Bugs
  • Discussion
  • Download
  • API Reference
  • Cookbook
  • Try jsoup
jsoup » Cookbook » Input » Read XML feeds from a URL

Read XML feeds from a URL

May 7, 2026

Problem

You need to fetch XML from the web and extract data from it. A common case is reading an RSS or Atom feed.

Solution

Use Jsoup.connect(String url).get(). When the server sends an XML content type, jsoup will use the XML parser automatically.

Document feed = Jsoup.connect("https://example.com/feed.xml").get();

for (Element entry : feed.select("channel > item, feed > entry")) {
    String title = entry.expectFirst("title").text();

    Element atomLink = entry.selectFirst("link[href]");
    Element rssLink = entry.selectFirst("link:not([href])");
    String link = atomLink != null ? atomLink.attr("abs:href") :
        rssLink != null ? rssLink.text() : "";

    Element date = entry.selectFirst("updated, published, pubDate");
    String dateText = date != null ? date.text() : "";

    System.out.printf("%s <%s> %s%n", title, link, dateText);
}

Description

The HTML parser is for HTML documents. It knows the HTML rules, and will build a normal HTML document around the input, with html, head, and body elements. It also knows that some HTML tags have special behavior. For example, in HTML, <link> is a metadata element and does not contain text.

That is not what you want for feeds. In RSS, <link> is often the element that contains the article URL:

<item>
  <title>New release</title>
  <link>https://example.com/news/new-release</link>
</item>

If that is parsed as HTML, the <link> element is treated like an HTML <link> tag. Its text will not be inside the link element. If it is parsed as XML, the tree stays as the feed wrote it, and entry.selectFirst("link").text() gives you the URL.

When a response has an XML content type, such as application/rss+xml, application/atom+xml, application/xml, text/xml, or another +xml type, jsoup switches from the HTML parser to the XML parser for that response. The XML parser does not add an HTML shell, and it preserves XML names and output.

RSS and Atom use different element shapes for links. RSS usually has a <link> element whose text is the URL. Atom usually has a <link href="..."> element. The example handles both in one loop.

If a server sends XML with the wrong content type, set the parser explicitly:

Document feed = Jsoup.connect("https://example.com/feed")
    .parser(Parser.xmlParser())
    .get();

Once parsed, you can use the same jsoup DOM methods, CSS selectors, and abs: URL resolution that you use with HTML.

Cookbook

Introduction

  1. Parsing and traversing a Document

Input

  1. Parse a document from a String
  2. Parsing a body fragment
  3. Load a Document from a URL
  4. Load a Document from a File
  5. Read XML feeds from a URL
  6. Extract data from a namespaced XML file
  7. Parse large documents efficiently with StreamParser

Extracting data

  1. Use DOM methods to navigate a document
  2. Use CSS selectors to find elements
  3. Use XPath selectors to find elements and nodes
  4. Extract attributes, text, and HTML from elements
  5. Working with relative and absolute URLs
  6. Example program: list links

Modifying data

  1. Set attribute values
  2. Set the HTML of an element
  3. Setting the text content of elements

Cleaning HTML

  1. Sanitize untrusted HTML (to prevent XSS)

Working with the web

  1. Maintaining a request session
jsoup HTML parser © 2009 - 2026 Jonathan Hedley