Start_Element 和 End_Element 未被调用

Start_Element and End_Element are not called

第一次尝试 xmlada,我很难让 SAX 模块工作。这是我要解析的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <name>Test project</name>
</project>

这是我使用的代码:

xml-project_loader.ads

with Ada.Strings.Unbounded;
with Sax.Attributes;
with Sax.Readers;
with Unicode.CES;

package XML.Project_Loader is
     
   type Reader is new Sax.Readers.Sax_Reader with null record;
   
   procedure Load (Filepath : String);

   procedure Start_Element
     (Handler       : in out Reader;
      Namespace_URI : Unicode.CES.Byte_Sequence := "";
      Local_Name    : Unicode.CES.Byte_Sequence := "";
      Qname         : Unicode.CES.Byte_Sequence := "";
      Atts          : Sax.Attributes.Attributes'Class);

   procedure End_Element
     (Handler       : in out Reader;
      Namespace_URI : Unicode.CES.Byte_Sequence := "";
      Local_Name    : Unicode.CES.Byte_Sequence := "";
      Qname         : Unicode.CES.Byte_Sequence := "");

   procedure Characters
     (Handler : in out Reader;
      Ch      : Unicode.CES.Byte_Sequence);
   
private
   Project_Reader : Reader;
   
end XML.Project_Loader;

xml-project_loader.adb

with Ada.Text_IO;
with Input_Sources.File;

package body XML.Project_Loader is

   procedure Load (Filepath : String)
   is
      Input  : Input_Sources.File.File_Input;
   begin
      Input_Sources.File.Open (Filepath, Input);
      Project_Reader.Parse (Input);
      Input.Close;
   end Load;
   
   procedure Start_Element
     (Handler       : in out Reader;
      Namespace_URI : Unicode.CES.Byte_Sequence := "";
      Local_Name    : Unicode.CES.Byte_Sequence := "";
      Qname         : Unicode.CES.Byte_Sequence := "";
      Atts          : Sax.Attributes.Attributes'Class)
   is
   begin
      Ada.Text_IO.Put_Line ("[Start_Element] " & Local_Name);
   end Start_Element;

   procedure End_Element
     (Handler       : in out Reader;
      Namespace_URI : Unicode.CES.Byte_Sequence := "";
      Local_Name    : Unicode.CES.Byte_Sequence := "";
      Qname         : Unicode.CES.Byte_Sequence := "")
   is
   begin
      Ada.Text_IO.Put_Line ("[End_Element] " & Local_Name);
   end End_Element;
   
   procedure Characters
     (Handler : in out Reader;
      Ch      : Unicode.CES.Byte_Sequence)
   is 
   begin
      Ada.Text_IO.Put_Line ("[Characters] " & Ch);
   end Characters;

end XML.Project_Loader;

当我调用过程 XML.Project_Loader.Load 时,我在控制台中得到的输出如下:

[Characters] Test project

过程Start_ElementEnd_Element没有被调用!我检查了 GDB,确实没有调用它们。

为什么没有调用程序?

因为它们不是遗传的。

你可以说

overriding
procedure Start_Element
  (Handler ....

这会给你一个警告。

问题是包中声明了两种reader:

   type Sax_Reader is tagged private;
   type Sax_Reader_Access is access all Sax_Reader'Class;
   --  This package defines two types of XML readers: Reader is the historic
   --  type; Sax_Reader was added later on.
   --  These two readers differ by the type of parameters to their callbacks.
   --  The callbacks of Sax_Reader require less string copying and memory
   --  allocations, so are therefore more efficient. On the other hand, they
   --  do not pass strings directly (for the name of the elements for instance)
   --  but symbols (basically, naturals that can be converted to a string
   --  through calls to Get_Symbol below).
   --  New code is encouraged to extend Sax_Reader rather than Reader.

之后,

   type Reader is new Sax_Reader with private;
   type Reader_Access is access all Reader'Class;
   --  This is the old type that was provided by this package

你的Reader

   type Reader is new Sax.Readers.Sax_Reader with null record;

需要新的 Start_ElementEnd_Element 配置文件,但您的配置文件是使用旧参数配置文件声明的。