How to resolve the algorithm XML/Output step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm XML/Output step by step in the Ada programming language

Table of Contents

Problem Statement

Create a function that takes a list of character names and a list of corresponding remarks and returns an XML document of elements each with a name attributes and each enclosing its remarks. All elements are to be enclosed in turn, in an outer element. As an example, calling the function with the three names of: And three remarks of: Should produce the XML (but not necessarily with the indentation): The document may include an declaration and document type declaration, but these are optional. If attempting this task by direct string manipulation, the implementation must include code to perform entity substitution for the characters that have entities defined in the XML 1.0 specification. Note: the example is chosen to show correct escaping of XML strings. Note too that although the task is written to take two lists of corresponding data, a single mapping/hash/dictionary of names to remarks is also acceptable. Note to editors: Program output with escaped characters will be viewed as the character on the page so you need to 'escape-the-escapes' to make the RC entry display what would be shown in a plain text viewer (See this). Alternately, output can be placed in tags without any special treatment.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm XML/Output step by step in the Ada programming language

Source code in the ada programming language

with Ada.Strings.Unbounded;
with Ada.Text_IO.Text_Streams;
with DOM.Core.Documents;
with DOM.Core.Elements;
with DOM.Core.Nodes;

procedure Character_Remarks is
   package DC renames DOM.Core;
   package IO renames Ada.Text_IO;
   package US renames Ada.Strings.Unbounded;
   type Remarks is record
      Name : US.Unbounded_String;
      Text : US.Unbounded_String;
   end record;
   type Remark_List is array (Positive range <>) of Remarks;
   My_Remarks : Remark_List :=
      ((US.To_Unbounded_String ("April"),
        US.To_Unbounded_String ("Bubbly: I'm > Tam and <= Emily")),
       (US.To_Unbounded_String ("Tam O'Shanter"),
        US.To_Unbounded_String ("Burns: ""When chapman billies leave the street ...""")),
       (US.To_Unbounded_String ("Emily"),
        US.To_Unbounded_String ("Short & shrift")));
   My_Implementation : DC.DOM_Implementation;
   My_Document       : DC.Document := DC.Create_Document (My_Implementation);
   My_Root_Node      : DC.Element  := DC.Nodes.Append_Child (My_Document,
                                         DC.Documents.Create_Element (My_Document, "CharacterRemarks"));
   My_Element_Node   : DC.Element;
   My_Text_Node      : DC.Text;
begin
   for I in My_Remarks'Range loop
      My_Element_Node := DC.Nodes.Append_Child (My_Root_Node,
                            DC.Documents.Create_Element (My_Document, "Character"));
      DC.Elements.Set_Attribute (My_Element_Node, "Name", US.To_String (My_Remarks (I).Name));
      My_Text_Node    := DC.Nodes.Append_Child (My_Element_Node,
                            DC.Documents.Create_Text_Node (My_Document, US.To_String (My_Remarks (I).Text)));
   end loop;
   DC.Nodes.Write (IO.Text_Streams.Stream (IO.Standard_Output),
                   N => My_Document,
                   Pretty_Print => True);
end Character_Remarks;


with Ada.Wide_Wide_Text_IO;

with League.Strings;
with XML.SAX.Attributes;
with XML.SAX.Pretty_Writers;

procedure Main is

   function "+"
    (Item : Wide_Wide_String) return League.Strings.Universal_String
       renames League.Strings.To_Universal_String;

   type Remarks is record
      Name   : League.Strings.Universal_String;
      Remark : League.Strings.Universal_String;
   end record;

   type Remarks_Array is array (Positive range <>) of Remarks;

   ------------
   -- Output --
   ------------

   procedure Output (Remarks : Remarks_Array) is
      Writer     : XML.SAX.Pretty_Writers.SAX_Pretty_Writer;
      Attributes : XML.SAX.Attributes.SAX_Attributes;
      
   begin
      Writer.Set_Offset (2);
      Writer.Start_Document;
      Writer.Start_Element (Qualified_Name => +"CharacterRemarks");

      for J in Remarks'Range loop
         Attributes.Clear;
         Attributes.Set_Value (+"name", Remarks (J).Name);
         Writer.Start_Element
           (Qualified_Name => +"Character", Attributes => Attributes);
         Writer.Characters (Remarks (J).Remark);
         Writer.End_Element (Qualified_Name => +"Character");
      end loop;
      
      Writer.End_Element (Qualified_Name => +"CharacterRemarks");
      Writer.End_Document;
      
      Ada.Wide_Wide_Text_IO.Put_Line (Writer.Text.To_Wide_Wide_String);
   end Output;

begin
   Output
    (((+"April",         +"Bubbly: I'm > Tam and <= Emily"),
      (+"Tam O'Shanter", +"Burns: ""When chapman billies leave the street ..."""),
      (+"Emily",         +"Short & shrift")));
end Main;


  

You may also check:How to resolve the algorithm Documentation step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Empty string step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm System time step by step in the Lasso programming language
You may also check:How to resolve the algorithm Comments step by step in the JCL programming language