XML files are plain text, so opening one is easy. Reading one is the harder part, because real-world XML is often a single dense line or thousands of nested elements. Here is how to open, read and check XML on every device.
Step 1: Know what kind of file it is
Some files with an .xml extension are data you can read directly: a sitemap, an RSS feed, a configuration file. Others are parts of bigger things. Office documents (.docx, .xlsx, .pptx) and .epub books are ZIP archives containing XML files, so they are not opened as XML at all. If a file you were sent has one of those extensions, open it with the appropriate application instead.
Step 2: Choose a viewer
A text editor. Any editor works. Notepad on Windows, TextEdit in plain-text mode on Mac, or a code editor. You get the raw text, and a code editor adds colouring and folding of nested elements.
A web browser. Drag the file into a browser window. Many browsers display XML as a collapsible tree, or show the raw source if it has no associated stylesheet, which is handy for a quick look. If the XML is not well-formed, a browser often shows a parse error naming the line and column.
Docento in the browser. Docento's Text & Markdown Editor opens .xml files on your device, without uploading them, in a fixed-width font with Find. It parses the file and shows "Valid XML" or an error with the line number as you type, and the Download button saves your edits.
On a phone. On Android, the Docento app opens .xml files from the file picker, from "Open with" and from Share. On iPhone, use the browser editor in Safari.
Step 3: Make it readable
A file with no line breaks can be a wall of angle brackets. Reformatting, called pretty-printing, adds indentation. Code editors have a format-document command, and command-line tools such as xmllint --format do the same. Formatting changes whitespace only. One caution: in XML, whitespace inside text content can be significant, so avoid reformatting documents where exact spacing matters, such as some mixed-content documents.
Step 4: Read the structure
- The first element after the declaration is the root.
- Indentation shows nesting: what is inside what.
- Attributes appear inside start tags, and child elements between the tags.
- Namespace prefixes, like
ns:title, tell you which vocabulary an element belongs to.
When the file is big, search for the element you need rather than scrolling.
If it will not open or parse
An XML parser stops at the first error. Typical causes:
- Unclosed or mismatched tags, such as
<a><b></a> - Unescaped special characters, particularly
&and<in text. See XML special characters and CDATA. - Unquoted attribute values
- More than one root element
- Text or whitespace before the XML declaration, including a stray byte order mark in the wrong place
- A declared encoding that does not match the real one. See text file encodings.
Read the reported line and the line before it. The terms are explained in well-formed vs valid.
Large XML files
Editors load the whole file. Files in the tens of megabytes may be slow or fail to open, and the Docento editors cap text files at 5 MB. For big files use a streaming tool or a command-line parser.
Safety note
XML can reference external resources through entities, which some parsers resolve. When you process XML from sources you do not trust, use a parser configured to disable external entities.
Editing
Make edits in an editor that preserves the encoding, keep the declaration accurate and re-check that the file is well-formed before saving over the original.
Takeaway
Any text editor opens an XML file, but a parser-aware tool makes it readable and tells you where it breaks. Check whether the file is really XML or a zipped format, format it for reading and fix errors from the first reported line.