PDF download Download Article
Easy tricks to view and use the contents of an XML file
PDF download 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.
Method 1
Method 1 of 4:

Using a Text Editor

PDF download Download Article
  1. On most versions of Linux, including Ubuntu, you can press Control + Alt + T to open a Terminal.
  2. 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
  3. 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.
  4. Advertisement
Method 2
Method 2 of 4:

Using a Web Browser

PDF download Download Article
  1. 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]
  2. A list of applications will appear. [2]
    • The wording here may vary by file manager.
  3. 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.
  4. Advertisement
Method 3
Method 3 of 4:

Converting to Another Format

PDF download Download Article
  1. On most Linux distributions, you can do this by pressing Control + Alt + T .
  2. 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 .
  3. 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.
  4. You'll use the cd command to do this. For example, cd /usr/local/files takes you to a directory called /usr/local/files.
  5. The command you'll use depends on the type of file you want to create:
    • xmlto pdf myfile.xml converts to a PDF. [3]
    • 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).
  6. Advertisement
Method 4
Method 4 of 4:

Parsing with XMLStarlet

PDF download Download Article
  1. 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.
  2. 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.
  3. 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
  4. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      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 71,465 times.

      Is this article up to date?

      Advertisement