如何使用 XML::LibXML 通过 SAX 解析 XML?

发布于 2024-08-02 20:02:54 字数 169 浏览 4 评论 0原文

到目前为止,我发现的唯一示例代码太旧了,无法再工作(使用已弃用的类)。我所需要的只是演示一些基本内容:

  1. 从文件加载并解析 XML

  2. 定义 SAX 事件处理程序

  3. 读取传递给事件处理程序的元素的属性或文本值

The only example code I have found so far is so old it won't work anymore (uses deprecated classes). All I need is something basic that demonstrates:

  1. Loading and parsing the XML from a file

  2. Defining the SAX event handler(s)

  3. Reading the attributes or text values of the element passed to the event handler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛徒 2024-08-09 20:02:54

发行版本身怎么样

转到 XML::LibXML 分发页面 并单击 浏览

请注意文档中的以下警告:

目前 XML::LibXML 只为 libxml2 的本机 SAX 实现提供了一个不完整的接口。目前的实现尚未在生产环境中进行测试。它可能会导致严重的记忆问题或显示错误的行为。

还有 XML::SAX 附带 不错的文档。我用过它几次,效果很好,达到了我的目的。

How about the distribution itself?

Go to XML::LibXML distribution page and click browse.

Note the following caution in the documentation:

At the moment XML::LibXML provides only an incomplete interface to libxml2's native SAX implementation. The current implementation is not tested in production environment. It may causes significant memory problems or shows wrong behaviour.

There is also XML::SAX which comes with nice documentation. I used it a few times and worked well for my purposes.

十年不长 2024-08-09 20:02:54

司南的建议很好,但并没有把所有的点联系起来。下面是我拼凑而成的一个非常简单的程序:

文件 1:处理程序 (MySAXHandler.pm)

  package MySAXHandler;
  use base qw(XML::SAX::Base);

  sub start_document {
    my ($self, $doc) = @_;
    # process document start event
  }

  sub start_element {
    my ($self, $el) = @_;
    # process element start event
    print "Element: " . $el->{LocalName} . "\n";
  }

1;

文件 2:测试程序 (test.pl)

#!/usr/bin/perl

use strict;
use XML::SAX;
use MySAXHandler;

my $parser = XML::SAX::ParserFactory->parser(
        Handler => MySAXHandler->new
);

$parser->parse_uri("some-xml-file.xml");

注意:如何获取元素属性的值。这没有以我可以使用的方式描述。我花了一个多小时才弄清楚语法。这里是。在我的 XML 文件中,属性是 ss:Index。 ss 的命名空间定义为 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"。因此,为了获得愚蠢的 Index 属性,我需要这个:

my $ssIndex = $el->{Attributes}{'{urn:schemas-microsoft-com:office:spreadsheet}Index'}{Value};

那很痛苦。

Sinan's suggestion was good, but it didn't connect all the dots. Here is a very simple program that I cobbled together:

file 1: The handlers (MySAXHandler.pm)

  package MySAXHandler;
  use base qw(XML::SAX::Base);

  sub start_document {
    my ($self, $doc) = @_;
    # process document start event
  }

  sub start_element {
    my ($self, $el) = @_;
    # process element start event
    print "Element: " . $el->{LocalName} . "\n";
  }

1;

file 2: The test program (test.pl)

#!/usr/bin/perl

use strict;
use XML::SAX;
use MySAXHandler;

my $parser = XML::SAX::ParserFactory->parser(
        Handler => MySAXHandler->new
);

$parser->parse_uri("some-xml-file.xml");

Note: How to get the values of an element attribute. This was not described in a way that I could use. It took me over an hour to figure out the syntax. Here it is. In my XML file, the attribute was ss:Index. The namespace definition for ss was xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet". Thus, in order to get the silly Index attribute, I needed this:

my $ssIndex = $el->{Attributes}{'{urn:schemas-microsoft-com:office:spreadsheet}Index'}{Value};

That was painful.

君勿笑 2024-08-09 20:02:54

XML::LibXML::Sax 实现 Perl SAX 接口 并且有一个很好的文档。

XML::LibXML::Sax implements the Perl SAX interface and there is a nice document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文