Linux Format

PYTHON/GO Parsing XML files

One of the easiest ways to learn a new programming language is to build a program first in a familiar language (Python) and then convert it to the language you want to learn (Go). That makes it easy to compare the features and idiosyncrasies of the two programming languages.

Figure 1 (right) shows the output of our Python program when reading an XML file. The Python parser, XMLParse.py, doesn’t have the ability to display comments in XML files. It knows how to ignore comments (after all, comments are for people, not programs), but not how to display them. Figure 2 (page

93) shows the output of our Go program, XMLParse. go, on the exact same XML file, postfix.xml. The Go version clearly does know how to display comments.

Let’s start by looking at XMLParse.py. Create a working directory and place XMLParse.py there along with the sample XML files, addressbook.xml and postfix.xml. Make sure that your version of XMLParse. py is executable. If it’s not, use the alias mx (alias mx=’chmod +x’) to make it executable for everyone. mx xmlParse.py ./xmlParse.py postfix.xml

In the main section, which every program must have, we first look at the command line arguments to check whether the user has entered the correct number of arguments and whether the user wants help. If so, we invoke the usage() function with an error

You’re reading a preview, subscribe to read more.

More from Linux Format

Linux Format2 min read
OBS Studio
Version: 30.0.2 Web: https://obsproject.com There are lots of good options for recording screencasts, but if you want to live-stream T your desktop, one of the best options is OBS Studio. The app works with all the major online streaming providers, s
Linux Format1 min read
Kaki Pi
Another Pi-like SBPC appears! From Yuridenk-Shokai in Japan, this packs a Renesas RZ/V2H Coretex-A55/R8/M33 MPU with 80 TOPS of AI. It’s aimed at robotics and has four CSI video inputs along with a PCIe 3 connection. Find out more in Japanese: www.ka
Linux Format2 min read
Back Issues Missed One?
ISSUE 313 April 2024 Product code: LXFDB0313 In the magazine Discover how to use the ultimate hacker’s toolkit, staying out of trouble while doing so. And join us as we take the Puppy Linux developer’s new distro for a run and explore its container

Related Books & Audiobooks