Here’s the translation of the XML example from Go to Ada, formatted in Markdown suitable for Hugo:
This Ada code demonstrates XML handling using the GNAT.XML.SAX package, which provides SAX (Simple API for XML) parsing capabilities. Here’s a breakdown of the code:
We define a Plant record type to represent our data structure.
The To_String function is implemented to provide a string representation of a Plant object.
We create a Coffee plant object with initial values.
A custom Plant_Handler type is defined, inheriting from the SAX Reader. This handler implements the Start_Element and Characters procedures to parse the XML content.
In the Start_Element procedure, we extract the “id” attribute when a “plant” element is encountered.
The Characters procedure is used to populate the Name and Origin fields of our Plant object.
In the main procedure, we first output the XML content as a string.
Then, we use the SAX parser to parse this XML content, which populates our Coffee object.
Finally, we print the parsed Plant object.
This example demonstrates basic XML handling in Ada, including creating XML content and parsing XML data. Note that Ada’s approach to XML handling is different from Go’s, as it uses a SAX parser instead of marshaling/unmarshaling to structs. However, this approach still allows for effective XML processing in Ada.
To run this program, you would typically compile it with a GNAT compiler and then execute the resulting binary:
This would output the XML content followed by the parsed Plant object information.