3
|
1 #!/usr/bin/perl
|
|
2 package MyParser;
|
|
3
|
|
4 use IMPL::declare {
|
4
|
5 require => {
|
|
6 XMLReader => 'XML::LibXML::Reader'
|
|
7 },
|
3
|
8 base => [
|
|
9 'Yours::Parsers::SaxParser' => undef
|
|
10 ]
|
|
11 };
|
|
12
|
4
|
13
|
3
|
14 sub ProcessRootNode {
|
|
15 my ($this,$node) = @_;
|
|
16
|
|
17 my $handler;
|
4
|
18 my $level = 1;
|
3
|
19 $handler = sub {
|
|
20 my ($me,$node) = @_;
|
|
21 print ' ' x $level, $node->depth, " ", $node->name,"\n";
|
|
22 $level++;
|
|
23 $me->ReadChildren($handler);
|
|
24 $level--;
|
|
25 };
|
|
26
|
|
27 print "root: \n";
|
|
28 $this->ReadChildren($handler);
|
|
29 }
|
|
30
|
|
31 my $text = '<?xml version="1.0"?>
|
4
|
32 <root xmlns:ns="asd">
|
|
33 <n0/>
|
|
34 <ns:n1 a="hi"></ns:n1>
|
3
|
35 <n2>
|
|
36 <w1>asd</w1>
|
|
37 </n2>
|
4
|
38 <n3 />
|
3
|
39 </root>
|
|
40 ';
|
|
41
|
4
|
42 my $reader = XMLReader->new({
|
3
|
43 string => $text
|
|
44 });
|
|
45
|
4
|
46 while ($reader->read) {
|
|
47 print " " x $reader->depth, "[", $reader->nodeType,"] ", $reader->name, "\n"
|
|
48 }
|
|
49
|
|
50 print "\n";
|
|
51
|
|
52 MyParser->new()->Parse({
|
|
53 string => $text
|
|
54 });
|
|
55
|