How to Import XML into Google Sheets
Bring an XML feed or API into a sheet with the right columns, from a one-off formula to a scheduled import that keeps the same tab up to date.
Where XML still turns up
Most new APIs answer in JSON, but XML is everywhere in older and public systems: RSS and Atom feeds, sitemaps, product feeds, government and banking data, payment and shipping carriers, and many accounting and inventory systems. The job is always the same: find the repeating element (each item, each url, each product) and turn each one into a row, with one column per child value.
There are three ways to do that in Google Sheets.
Way 1: IMPORTXML with XPath
IMPORTXML takes an address and an XPath query and returns what matches:
=IMPORTXML("https://example.com/feed.xml", "//item/title")
That gives one column of titles. To get several columns from the same items, run one formula per column side by side:
A1: =IMPORTXML("https://example.com/feed.xml", "//item/title")
B1: =IMPORTXML("https://example.com/feed.xml", "//item/link")
C1: =IMPORTXML("https://example.com/feed.xml", "//item/pubDate")
This works until one item is missing a field. Then column C is one row shorter than column A and every date after it sits next to the wrong title. XPath can't keep a row together across separate formulas. Use it for flat feeds where every item has every field, or for a single value.
Two more things to know:
- Namespaces. Sitemaps, Atom feeds and many APIs declare a default namespace, and then
//url/locmatches nothing.//*[local-name()='loc']matches the element by its own name and works either way. - Refresh and size. IMPORTXML checks for updates about once an hour, only while the file is open, and answers "Result too large" when a query matches too much. The import formulas guide covers those limits and the Allow access banner.
For RSS and Atom specifically, IMPORTFEED is simpler: =IMPORTFEED("https://example.com/feed.xml", "items", TRUE, 50) returns the 50 newest items with headers.
Way 2: Apps Script and XmlService
A script keeps each record together, because it reads the repeating element first and the values inside it second. This one turns a sitemap into a tab with the address and last-modified date of every page:
function importSitemap() {
const xml = UrlFetchApp.fetch('https://example.com/sitemap.xml').getContentText();
const root = XmlService.parse(xml).getRootElement();
const ns = root.getNamespace();
const rows = root.getChildren('url', ns).map((u) => [
u.getChildText('loc', ns),
u.getChildText('lastmod', ns) || '',
]);
const sh = SpreadsheetApp.getActive().getSheetByName('Sitemap');
sh.clearContents();
sh.getRange(1, 1, 1, 2).setValues([['Address', 'Last modified']]);
if (rows.length) sh.getRange(2, 1, rows.length, 2).setValues(rows);
}
Passing the namespace (ns) to getChildren and getChildText is the script version of the local-name trick. A missing lastmod becomes a blank cell instead of shifting the column. Add a time trigger and it refreshes while the file is closed. What you still write yourself: headers for APIs that need a key, pages for feeds that split into pages, and date conversion (that lastmod arrives as text, not a date).
Way 3: a scheduled import that reads XML
Cellfetch reads XML responses the same way it reads JSON, so everything else (keys, pages, schedules, write modes) works unchanged. It turns the document into a tree you pick from:
- Each element becomes a field named after it; an element with only text becomes that text.
- Attributes become
@name, so<price currency="USD">19.99</price>givesprice.@currencyandprice.#text. - An element that repeats becomes a list, in document order. That's the one you choose as the records.
- Namespace prefixes stay as written, such as
dc:creator, so nothing collides.
For the sitemap above, the records sit at $.urlset.url and the columns are loc and lastmod. For an RSS feed, the records are $.rss.channel.item and the columns title, link, pubDate and dc:creator. The paths use the same JSONPath notation as JSON imports; the JSONPath glossary entry explains it on a real response.
On the safety side, Cellfetch never expands entities declared inside an XML file (so a hostile file can't balloon in memory or reach other addresses), and it stops with a plain message and a line number when a file is malformed, deeper than 256 levels, or larger than the 50 MB Apps Script itself can fetch.
Worked example: a supplier's product feed
A supplier publishes 1,800 products as XML at one address, refreshed each night, behind a key sent as a header. IMPORTXML can't send the key. A script can, but the feed has 40 fields per product and some products skip half of them. As a saved request: the address, the key in a connection, records at $.catalog.product, the 8 columns the buyer actually uses, and a daily run at 6 AM in replace mode. The tab is complete before anyone opens it, and a product that lacks a field gets a blank in that column rather than shifting the row.
If the data turns out to be JSON after all, the import JSON into Google Sheets guide covers the same ground for JSON. For APIs in general, start with how to use an API in Google Sheets.
Questions
Why does IMPORTXML return nothing for my sitemap?
Sitemaps declare a default namespace, and an XPath such as //url/loc only matches elements with no namespace. Match on the local name instead: //*[local-name()='loc'] works whether or not the file declares one.
Can IMPORTXML send a username, password or API key?
No. It takes an address, an XPath query and an optional locale. XML APIs that need a key need Apps Script or an add-on that can send headers.
How big an XML file can Apps Script fetch?
UrlFetchApp's limit is 50 MB per response. Parsing a file that size with XmlService is slow, and a run stops after 6 minutes, so large feeds are better fetched in pages or by date.
Does Cellfetch import XML web pages?
No, only XML files, feeds and APIs. Web pages are HTML; for a table on a public page, IMPORTHTML is the right tool.
More guides
From inside a sheet: Extensions > Add-ons > Get add-ons, search Cellfetch, Install. Then Extensions > Cellfetch > Start.