How to use XML :: LibXML to parse XML using SAX?

The only code sample I have found so far is so old that it will no longer work (uses obsolete classes). All I need is something basic that demonstrates:

  • Loading and parsing XML from a file

  • Defining a SAX Event Handler

  • Reading the attributes or text values ​​of an element passed to an event handler

+3
source share
3 answers

How about the spread itself ?

Go to the XML Distribution Page :: LibXML page and click Browse .

:

XML:: LibXML libxml2 native SAX. . .

XML::SAX, . .

+7

, . , :

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". , , :

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

.

+6

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

0
source

Source: https://habr.com/ru/post/1716033/


All Articles