Convert xml to flat format in Ubuntu distribution

In this article, we would first discuss what flat format is all about. Thereafter, we would install necessary packages and convert xml to flat format in Ubuntu distribution.

To begin with, XML is an abbreviation for Extensible Markup Language. A simple example of XML text can be –

<note>
     <to>XYZ</to>
     <from>ABC</from>
     <heading>Hello</heading>
     <body>An XML example</body>
</note>

The flat format equivalent of the same is –

​/note/to=XYZ
/note/from=ABC
/note/heading=Hello
/note/body=An XML example

Flat format helps us to process the structured markup efficiently when using line-oriented tools.

Now, we would discuss how to install xml2 package.

Note: Following operations would require you to have superuser privileges. In case you don’t have one then contact your System Administrator for assistance.

Install xml2 in Ubuntu distribution

The package xml2 is already available in standard Ubuntu repository. Therefore, we need to update the repository to get the latest version of the package available. Hence, issue the following in terminal –

sudo apt update

Next, install the relevant package and associated dependencies (if any) –

sudo apt install xml2

Now, we will discuss how to convert the relevant files to required file formats.

Convert xml to flat format in Ubuntu

To convert xml to flat format, we would use command-line utility xml2. We would discuss it with the help of an example. Lets say, we have xml_file.xml and we need to the convert it into a flat format file i.e. flat_file

Then, issue the following in terminal –

xml2 < xml_file.xml > flat_file

We would notice that the content in our output file i.e. flat_file would be different to that of our input file.

In general, its

xml2 < input_file > output_file

Likewise, we can convert flat format file to xml. It will be discussed next.

Convert flat format to xml in Ubuntu

To convert flat format to xml, we would use command-line tool 2xml. Again consider –

input file – flat_file

output file – xml_file.xml

Now, issue the following in terminal to convert flat_file to xml_file.xml –

2xml < flat_file > xml_file.xml

In general, its

2xml < input_file > output_file

In conclusion, we have discussed how to convert xml to flat format and vice versa using command-line utilities xml2 and 2xml.