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.
Start your free 30 days