Download Article
Easy tricks to view and use the contents of an XML file
Download Article
Files that end in the .xml file extension are plain text files containing XML code. Rather than "execute" them like you would a program or script, you'd simply open it in one of two ways—in a text editor or a web browser. You can also convert the file to a different format using a converter like xmlto, or use a tool like Xmlstarlet to parse XML files in your bash scripts.
Things You Should Know
- You can open XML files in any text editor such as Nano or Vim.
- To view your XML file in a web browser, right-click the file, select "Open With Other Application," and select your web browser.
- If you want to parse XML in your bash scripts, install the XMLStarlet command line toolkit.
Steps
-
Open a Terminal window. On most versions of Linux, including Ubuntu, you can press Control + Alt + T to open a Terminal.
-
Navigate to the directory containing your XML file. You'll use the cd command to do this. For example, cd /home/wikihow/Desktop/xmlDocs takes you to a directory called "xmlDocs" in the wikihow user's home directory.Advertisement
-
Open the file in a text editor. For example, to use the Nano text editor to view and edit the XML file, you'd type nano <filename> , replacing <filename> with the name of the XML file. Once open, you can view and edit the XML file's contents.
- If you just want to display the contents without opening the file in a text editor, you can also use the command more <filename> to view the contents screen by screen, or cat <filename> to display the entire file at once.
Advertisement
-
Navigate to the .xml file you want to view. If you're using Ubuntu, you might use the Nautilus file manager, also known as Files. If you're using KDE, the built-in file manager is Dolphin. [1] X Research source
-
Right-click the file and select Open With Other Application . A list of applications will appear. [2] X Research source
- The wording here may vary by file manager.
-
Select your web browser. Firefox comes preinstalled on many Linux flavors, but you can use any modern web browser—they can all display XML files. This will automatically display the contents of the file.
Advertisement
-
Open a Terminal window. On most Linux distributions, you can do this by pressing Control + Alt + T .
-
Update your distribution's repositories. To make sure you're downloading the latest version of xmlto, the tool you'll be using to convert the XML file to plain text, run the command to update your package manager's source list.
- For example, on a Debian-based system like Ubuntu , run the command sudo apt update .
-
Install the xml2 package. To install xmlto, run the command sudo apt install xmlto .
- If you're using a different package manager, such as Dnf or Yum, replace "apt" with "dnf," "yum," or your package manager's name.
-
Navigate to the directory containing your XML file. You'll use the cd command to do this. For example, cd /usr/local/files takes you to a directory called /usr/local/files.
-
Run the command to convert the file to another format. The command you'll use depends on the type of file you want to create:
- xmlto pdf myfile.xml converts to a PDF. [3] X Research source
- To convert to another file type, replace pdf with the desired file type, such as: html , htmlhelp (HTML help file), javahelp (Java help file), dvi (text DVI file), man (Unix manual pages), txt (plain text), ps (postscript), xhtml , fo (XSL-FO formatting objects).
Advertisement
-
Install XMLStarlet. If your goal is to parse the data in an XML file in your bash scripts or from the command line, this tool makes it easy. You can use XMLStarlet in your scripts to query, transform, or check the data in any XML file.
- This tool is installed by default if you're using RedHat or CentOS. On most other distributions, you'll need to install it from your package manager.
- On Ubuntu, Debian, and Linux Mint, use the command sudo apt install xmlstarlet to install the package.
-
Run xmlstarlet --help to view options and commands. This displays a list of all commands you can use to edit, select, transform, validate, and format your XML file. The commands and options you'll need will vary depending on what you need to do with the file, but the syntax will always be the same: xmlstarlet <command> [<cmd-options>] <filename> .
- Command values
- ed: Edit the XML file.
- sel: Select data or query the file.
- tr: Transform the file with XSLT.
- val: Validate the file.
- fo: Format the file.
- el: Display the file's element structure.
- c14n: XML canonicalization.
- ls: List a directory as XML.
- esc: Escape special XML characters.
- unesc: Unescape special characters.
- pyx: Convert to XML to PYX.
- p2x: Convert PYX to XML.
- Command options:
Each command also has its own options. To view a command's options, use xmlstarlet <command> --help
.
- For example, to see all options you can use with the fo command, use xmlstarlet fo --help .
- General option values:
You can add any of these after the xmlstarlet
command to issue some global options:
- -q: Don't output errors.
- --doc-namespace: Extract namespace bindings from input file.
- --no-doc-namespace: Don't extract namespace bindings.
- Command values
-
Run your XMLstarlet commands in the bash shell. For example, if you want to view the values of the XML nodes name and birthdate , you'd use the sel command with the -t (template) and -v (value of) options.
- Let's say you wanted to print the values of the <name>
and <birthdate>
nodes in an xml file called text.xml
. In this case, you'd use:
- xmlstarlet sel -t -v xml/name/birthdate test.xml
- Let's say you wanted to print the values of the <name>
and <birthdate>
nodes in an xml file called text.xml
. In this case, you'd use:
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Tips
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
References
About This Article
Article Summary
X
1. Open the file in a web browser if you just want to view it.
2. To edit the file, open it in VIM.
3. Use xtmlto to convert the file to another format.
Did this summary help you?
Thanks to all authors for creating a page that has been read 79,001 times.
Advertisement