【问题标题】:Start_Element and End_Element are not called不调用 Start_Element 和 End_Element
【发布时间】:2022-02-06 04:14:24
【问题描述】:

第一次尝试 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,确实没有调用它们。

为什么不调用程序?

【问题讨论】:

  • 西蒙已经回答了原因。只是想扔进去,您可以在任何方法覆盖前加上关键字:覆盖,这将迫使编译器告诉您签名是否不匹配。它不是必需的,但对于在使用类型扩展时查找错误非常有用.. EX:覆盖过程 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);
  • 或者,ofc,not overriding 以确保您不是!如果您认为您可能想要将子程序添加到父类型,则主要有用。

标签: xml ada


【解决方案1】:

因为他们没有继承。

你可以说

overriding
procedure Start_Element
  (Handler ....

这会给你一个警告。

问题是包中声明了两种阅读器:

   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 配置文件,但您的配置文件是使用旧参数配置文件声明的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2016-05-16
    • 1970-01-01
    • 2021-09-23
    • 2021-02-28
    • 2019-06-17
    • 1970-01-01
    相关资源
    最近更新 更多