How to Parse XML on Linux Command Line

0

Unlike different dossier sorts, XML is structured, and while you print its contents at the command line, you might imagine it’s unreadable. However, that’s how XML is structured, and the most suitable choice is to parse it the use of the to be had command line gear. While maximum imagine parsing XML is difficult, this information will smash down the complexity by means of introducing two simple gear to ease your XML parsing at the Linux command line. Let’s have a look.

Two Ways of Parsing XML on Linux Command Line

XML stands for Extensible Markup Language. It works as a dossier layout and a markup language, making it helpful for transmission, garage, and different functionalities. The primary problem with XML is its issue to learn because of its construction, and until you may have a blank manner of parsing it, a unmarried XML line can simply be complicated.

Take a take a look at the next symbol. Understanding what it approach may also be tricky, however we can see the best way to repair that and parse it the use of two gear.

Method 1. Using the xmllint Command

The xmllint is a competent XML formator and validation instrument. To use the instrument, you will have to set up the libxml2-utils bundle.

With xmllint put in, let’s continue and parse our XML dossier. Here is the next syntax for the use of xmllint:

$ xmllint [options] xml-file

The very first thing when parsing your XML dossier is to validate your XML is easily written. For this, use the next command and optionally upload the –noout to steer clear of printing the XML’s contents however most effective validate it:

$ xmllint –noout filename

If you may have an error for your XML dossier, you’re going to get an error output in your terminal, as proven within the following instance:

Remember the output that was once onerous to learn ahead of? You can parse it the use of xmllint and get a beautiful output in your command line. For that, use the –layout argument and notice how smartly formatted your XML dossier gets revealed.

With xmllint, you’ll additionally exchange the indentation on your dossier and make a selection what number of areas you need for the indent. However, you will have to create an atmosphere variable, XMLLINT_INDENT, and set the selection of areas you want to use.

For example, if we wanted 5 areas, the command to export the surroundings variable could be:

$ export XMLLINT_INDENT=” “

Now, your XML gets formatted with the desired indentation areas.

If you understand your XML dossier has useless areas, you’ll take away them the use of the –noblanks argument, which gets rid of even the newlines.

$ xmllint –noblanks filename

You will notice eliminating the areas will disorient the layout of your XML, however you’ll use this selection when you wish to have to cut back the dimensions of your XML report.

Method 2. Using the XMLStarlet Command

The XMLStarlet is any other dependable instrument for parsing XML by way of the command line. It has various choices that you’ll use to become, parse, question, or validate your XML dossier. You will have to set up the command line instrument to make use of it and on Ubuntu, use the command supplied underneath:

$ sudo apt set up xmlstarlet

With XMLStarlet, you’ll simply extract knowledge out of your XML and carry out different easy actions

Everything you’ll do with the command line instrument is to be had on its lend a hand web page. Let’s see a number of commonplace utilization examples.

To view an XML dossier, use the next command:

$ xmlstarlet layout filename

If you had to validate the XML, the command is equipped underneath:

$ xmlstarlet val filename

To choose knowledge with XMLStarlet, use the choose possibility and specify the trail to the node. In our XML dossier, use the next command to make a choice the cope with:

$ xmlstarlet choose –template –value-of /shoppers/buyer/cope with –nl test3.xml

In the former command, the –nl specifies so as to add a brand new line after the output. You can adjust the trail to get any explicit knowledge within the XML

There are more than a few techniques you’ll specify the XMLStarlet choices to paintings together with your XML dossier, and the great choices are at the guy web page for the command line.

Conclusion

Parsing XML information shouldn’t be difficult when the use of Linux. You will experience operating with XML information you probably have the appropriate command line gear to get the task completed. This information all in favour of two command line choices for parsing XML information. Try them out and spot which you in finding more straightforward to make use of.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More